简体   繁体   English

为什么我的RelayCommand不会触发并且ObservableCollection捕获所选值?

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

I am new to using RelayCommands (following Josh Smith's MVVMDemoApp) and could use some help identifying my mistake. 我是使用RelayCommands(在Josh Smith的MVVMDemoApp之后)的新手,可以使用一些帮助来识别我的错误。

I have two listboxes. 我有两个列表框。 When an item in the first is selected and the "Add" button is pressed, the AddCommand is executed and the second list's ObservableCollection gets the selectedItem added to it. 当选择第一个中的项目并按下“添加”按钮时,将执行AddCommand,第二个列表的ObservableCollection会将selectedItem添加到其中。

My View: 我的观点:

<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>

My ViewModel: 我的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);

    }

Thanks in advance! 提前致谢!

Also: why would: 另外:为什么会:

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

leave my Add button permanently disabled? 永久禁用我的“添加”按钮? What am I not doing to let the button know that an item has been selected? 我该怎么做才能让按钮知道已经选择了一个项目? This seem like it is the same missing link to my understanding of why the command isn't firing ever. 看来,这是我了解为何命令永远不会触发的同一缺失链接。

Thanks again! 再次感谢!

You need to raise the CanExecuteChanged on your command to let the client know that it should check again to see if it can execute. 您需要在命令上引发CanExecuteChanged ,以使客户端知道应该再次检查以查看其是否可以执行。 Not sure about the RelayCommand but I would assume it's something along the lines of mycommand.RaiseCanExecuteChanged(); 不确定RelayCommand但我认为这与mycommand.RaiseCanExecuteChanged(); Don't forget to cast your command to a relaycommand first since you have it exposed as ICommand . 不要忘记先将命令转换为中继命令,因为您将其公开为ICommand

OOPS! 糟糕! Right after posting this after an hour of struggling I realized that in my View I was referring to the selectedItem property "SelectedPartNumber" and not "SelectedPN". 经过一个小时的努力,在发布此内容后,我立即意识到,在我的视图中,我指的是selectedItem属性“ SelectedPartNumber”而不是“ SelectedPN”。 This solved both problems. 这解决了两个问题。 CanExecuteChanged is evaluated already. CanExecuteChanged已评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM