鉴于以下 XAML...
<ComboBox x:Name="advisoriesComboBox"
DisplayMemberPath="Name"
ItemsSource="{Binding Path=Advisories}"
SelectedItem="{Binding Path=SelectedAdvisory}" />
<Button Command="{Binding Path=AddAdvisoryCommand}"
CommandParameter="{Binding ElementName=advisoriesComboBox, Path=SelectedItem}"
CommandTarget="{Binding ElementName=advisoriesComboBox}"
Content="Add..." />
我正在寻找一种方法来绑定 ComboBox、Button 和 Command,这样当 ComboBox 的值发生变化时,就会在 Command 上调用 CanExecute。我想要的最终效果是能够根据在列表中选择的项目启用和禁用按钮,我更喜欢使用 ICommand 界面来执行此操作。
我过去通过使用 VM 上的“SelectedAdvisory”属性并在命令对象上手动调用 RaiseCanExecuteChanged 来完成此操作(我使用的是 PRISM v4 中的 DelegateCommand 实例),但我确信有更好、更清洁的方法来做这仅使用 XAML。
谢谢。
编辑:此外,是否有更简单的方法可以从 Button 引用 ComboBox?我尝试使用 RelativeSource PreviousData 但无法使其工作,因此使用x:Name
.
再次感谢。