C# 将命令绑定到WPF中的ComboBoxItem
我有一个简单的组合框,每次选定值更改时,我都希望触发单独的命令。以下是我的标记示例:C# 将命令绑定到WPF中的ComboBoxItem,c#,wpf,combobox,C#,Wpf,Combobox,我有一个简单的组合框,每次选定值更改时,我都希望触发单独的命令。以下是我的标记示例: <WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleViewFeedViewManual}"> <ComboBox Margin="3,3,0,0"> <ComboBoxItem IsEnabled="{Binding CanSelectViewFeedData}" >
<WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleViewFeedViewManual}">
<ComboBox Margin="3,3,0,0">
<ComboBoxItem IsEnabled="{Binding CanSelectViewFeedData}" >
<ComboBoxItem.CommandBindings>
<CommandBinding Command="SelectViewFeedDataCommand" />
</ComboBoxItem.CommandBindings>
<TextBlock Text="View Feed Data"/>
</ComboBoxItem>
<ComboBoxItem IsEnabled="{Binding CanSelectViewManualData}">
<ComboBoxItem.CommandBindings>
<CommandBinding Command="SelectManualFeedDataCommand" />
</ComboBoxItem.CommandBindings>
<TextBlock Text="View Manual Data"/>
</ComboBoxItem>
</ComboBox>
</WrapPanel>
我已经对此进行了相当广泛的研究,但还没有找到如何有效地将ICommand绑定到ComboBoxItem的答案
我是从使用一组单选按钮和相关命令的现有代码中改编的,这很容易做到。有没有简单的方法可以用组合框来实现这一点
谢谢。您是否尝试将命令放入绑定
<CommandBinding Command="{Binding SelectManualFeedDataCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
编辑以更新策略:
由于ComboBox项不支持直接命令绑定,请尝试创建附加属性:
<WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleFeedViewManual}" >
<ComboBox Margin="10,10,10,10" Width="200" ItemsSource="{Binding AvailableDataSources}" SelectedValue="{Binding SelectedDataSource}"/>
</WrapPanel>
我在看我以前的帖子时意识到我从来没有发布过我问题的解决方案。。。非常简单(如果不是优雅的话)。我刚刚在我的ViewModel中创建了一个列表,其中包含我的组合框“choices”,然后当SelectedValue发生更改时,在属性的setter中触发了我的“changed”逻辑:
<WrapPanel Grid.Row="0" Visibility="{Binding ShowToggleFeedViewManual}" >
<ComboBox Margin="10,10,10,10" Width="200" ItemsSource="{Binding AvailableDataSources}" SelectedValue="{Binding SelectedDataSource}"/>
</WrapPanel>
是的,我有。我收到一条错误消息:“不能在“CommandBinding”类型的“Command”属性上设置“Binding”。只能在DependencyObject的DependencyProperty上设置“Binding”。如何绑定组合框。选择EdItem属性到某个对象,然后在SelectedComboxItem属性的
PropertyChanged
事件中执行命令逻辑?
public FeedSource SelectedDataSource
{
get { return _selectedDataSource; }
set
{
_selectedDataSource = value;
base.OnPropertyChanged("SelectedDataSource");
//additional code to perform here
}
}