簡體   English   中英

為什么我的RelayCommand不會觸發並且ObservableCollection捕獲所選值?

[英]Why won't my RelayCommand fire and ObservableCollection catch selected value?

我是使用RelayCommands(在Josh Smith的MVVMDemoApp之后)的新手,可以使用一些幫助來識別我的錯誤。

我有兩個列表框。 當選擇第一個中的項目並按下“添加”按鈕時,將執行AddCommand,第二個列表的ObservableCollection會將selectedItem添加到其中。

我的觀點:

<DockPanel >

    <Border DockPanel.Dock="Bottom" Height="50" HorizontalAlignment="Left" Width="150" >
        <!--Notice here that the Button was disabled until it was given a DataContext, which allowed the CanAddPN to be true-->
        <Button Command="{Binding Path=AddToPartsBinCommand}" Content="Add >" />
    </Border>

    <UniformGrid Columns="2" Rows="1" DockPanel.Dock="Top" >
        <!--ListBox 1 (PartNumbersCollection)-->
        <ListBox Background="PaleGoldenrod"
                 ItemsSource="{Binding PnsCollection, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                 SelectedItem="{Binding SelectedPartNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding pn}">

                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <!--ListBox 2 (SelectedPartNumbersCollection)-->
        <ListBox ItemsSource="{Binding PartsBinCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding pn}">

                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </UniformGrid>


</DockPanel>

我的ViewModel:

 //DummyDBEntities _context;
    public ObservableCollection<PartNumber> _pnsCollection;
    public ObservableCollection<PartNumber> _partsBinCollection;

    PartNumber _selectedPN;
    public ICommand _addToPartsBinCommand;



    public MasterViewModel(DummyDBEntities _context)
    {
        _context = new DummyDBEntities();
        this._pnsCollection = new ObservableCollection<PartNumber>(_context.PartNumbers);
        this._partsBinCollection = new ObservableCollection<PartNumber>();



    }




    public ObservableCollection<PartNumber> PnsCollection
    {
        get { return this._pnsCollection; }
        set
        {
            _pnsCollection = value;
            OnPropertyChanged("PnsCollection");
        }
    }

    public PartNumber SelectedPN
    {
        get { return this._selectedPN; }
        set 
        {
            this._selectedPN = value;
            OnPropertyChanged("SelectedPN");
            OnPropertyChanged("PartsBinCollection");
        }

    }

    public ObservableCollection<PartNumber> PartsBinCollection
    {
        get
        {
            if (_partsBinCollection == null)
            {
                _partsBinCollection = new ObservableCollection<PartNumber>();
            }
            return this._partsBinCollection;
        }
        set 
        {
           this._partsBinCollection = value;
            OnPropertyChanged("PartsBinCollection");
        }
    }


    public ICommand AddToPartsBinCommand
    {
        get
        {
            if (_addToPartsBinCommand == null)
                _addToPartsBinCommand = new RelayCommand(() => this.AddPN(),
                                                         () => this.CanAddPN());
            return this._addToPartsBinCommand;
        } 
    }

    private bool CanAddPN()
    {
        return true;
    }


    private void AddPN()
    {
        if (this._partsBinCollection == null)
        {
            this._partsBinCollection = new ObservableCollection<PartNumber>();
        }

        this._partsBinCollection.Add(this._selectedPN);

    }

提前致謝!

另外:為什么會:

   private bool CanAddPN()
    {
        return this._selectedPN != null;
    }

永久禁用我的“添加”按鈕? 我該怎么做才能讓按鈕知道已經選擇了一個項目? 看來,這是我了解為何命令永遠不會觸發的同一缺失鏈接。

再次感謝!

您需要在命令上引發CanExecuteChanged ,以使客戶端知道應該再次檢查以查看其是否可以執行。 不確定RelayCommand但我認為這與mycommand.RaiseCanExecuteChanged(); 不要忘記先將命令轉換為中繼命令,因為您將其公開為ICommand

糟糕! 經過一個小時的努力,在發布此內容后,我立即意識到,在我的視圖中,我指的是selectedItem屬性“ SelectedPartNumber”而不是“ SelectedPN”。 這解決了兩個問題。 CanExecuteChanged已評估。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM