我对此束手无策......所以我有一个经典的产品类别场景。产品属于一个类别。我的列表框来源是产品,每个产品将再次有一个类别组合框。我的问题是,SelectionChanged 似乎没有触发。所有数据都显示正常。组合框加载正常等。不知道如何调试这个.. .......
<DataTemplate x:Key="ProductDataTemplate">
<StackPanel Orientation="vertical">
//resources here...
<TextBlock Text="{Binding Path=ProductName}" ></TextBlock>
<ComboBox Background="Transparent" SelectedValuePath="CategoryId" DisplayMemberPath="CategoryName" SelectedIndex="0">
<ComboBox.ItemsSource>
<CompositeCollection>
<models:Category CategoryName="Select" CategoryId="{x:Static sys:Guid.Empty}" Order="0"/>
<CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=categories}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path=SelectionChangedCommand}" CommandParameter="{Binding SelectedItem}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</StackPanel>
我在其他地方的列表框是:
<ListBox ItemsSource="{Binding Products}" ItemTemplate="{DynamicResource ProductDataTemplate}" Height="Auto" Margin="50,256,50,64" HorizontalAlignment="Center">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
我的视图模型是标准的东西:
public ICommand SetSelectionChangedCommand { get { return new RelayCommand(param => this.SetSelectionChangedExecute(param), null); } }
private void SetSelectionChangedExecute(object param)
{
MessageBox.Show("Selected");
}
我所拥有的有什么问题..为什么当我为随机产品选择随机类别时,处理程序无法控制?我的 RelayCommand 下面:
public class RelayCommand : ICommand
{
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
}
编辑: 我感觉我的问题是命令绑定路径不正确。它怎么知道去查看 ViewModel?我的 ViewModel 被设置为在层次结构中向上的祖先 Grid 的 DataContext。够了吗?还是我必须正确限定命令绑定中的路径?:
<i:InvokeCommandAction Command="{Binding Path=SelectionChangedCommand}" CommandParameter="{Binding SelectedItem}"></i:InvokeCommandAction>
我必须做些什么来确保在我的 ViewModel 中搜索命令?