为什么获胜';t我的RelayCommand fire和ObservableCollection捕获选定值
本文关键字:ObservableCollection fire RelayCommand 获胜 我的 为什么 | 更新日期: 2023-09-27 17:58:57
我是使用RelayCommands的新手(遵循Josh Smith的MVVMDemoApp),可能需要一些帮助来识别我的错误。
我有两个列表框。当选择第一个列表中的一个项目并按下"添加"按钮时,执行AddCommand,第二个列表的ObservableCollection会将所选项目添加到其中。
我的观点:
<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>
我的视图模型:
//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();
类似不要忘记先将命令强制转换为relaycommand,因为它已公开为ICommand
。
OOPS!经过一个小时的努力,我在发布这篇文章后立即意识到,在我的视图中,我指的是selectedItem属性"SelectedPartNumber",而不是"SelectedPN"。这解决了这两个问题。CanExecuteChanged已评估。