2013-11-04 49 views
0

我在这个机智的结尾...所以我有一个经典的产品类别场景。产品属于一个类别。我的列表框源代码是产品,每个产品都会有一个类别的组合框。我的问题是,SelectionChanged似乎并没有触发。所有数据都显示正常。组合框加载罚款等不知道如何调试这个.. .......列表框中Combobox的事件处理(使用数据绑定)

<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被设置为一个祖先网格的DataContext,它在层次结构中处于领先地位。够了吗?或者,我要好好地限定在命令中的路径绑定?:

    <i:InvokeCommandAction Command="{Binding Path=SelectionChangedCommand}" CommandParameter="{Binding SelectedItem}"></i:InvokeCommandAction> 

什么我必须做,以确保命令中搜索在我的ViewModel?

+1

我试过同在一个示例应用程序,它似乎工作的罚款。你可以从'RelayCommand'发布'ICommand.CanExecute'代码吗? – sthotakura

+0

@sthotakura问题用RelayCommand更新。在其他地方,命令都可以正常工作。但不是在这里.. – Brian

+0

代码看起来对我来说还行。 – sthotakura

回答

0

的问题是,我们需要正确指定路径的命令绑定:

     <i:InvokeCommandAction Command="{Binding Path=DataContext.SelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=1}}" CommandParameter="{Binding SelectedItem}"></i:InvokeCommandAction> 
1

看来我发现不一致,同时检查代码的实现,你拿给我们:在产品的DataTemplate实施

SelectionChanged事件触发,你在命令结合SelectionChangedCommand

但是,我看到,在视图模型,则的SelectionChanged命令被声明为SetSelectionChangedCommand。您在ViewModel的命令定义中添加了一个'设置'前缀。

考虑到绑定如果未找到路径引用,则不会引发任何异常。相反,它会起到触发不起任何作用的不存在的命令的作用,并且应用程序将继续没有任何警告。

我希望这会有所帮助。

+0

你是绝对正确的。我修正了这一点。仍然没有运气。还有一些我缺少.. – Brian

相关问题