0

私はこれで頭がいっぱいです...だから、古典的な製品カテゴリのシナリオがあります。製品はカテゴリに属しています。私のリスト ボックスのソースは製品で、各製品には再びカテゴリのコンボ ボックスがあります。私の問題は、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>

Command が ViewModel で確実に検索されるようにするにはどうすればよいですか?

4

2 に答える 2

0

問題は、コマンド バインディングでパスを適切に指定する必要があることです。

                        <i:InvokeCommandAction Command="{Binding Path=DataContext.SelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=1}}" CommandParameter="{Binding SelectedItem}"></i:InvokeCommandAction>
于 2013-11-05T22:35:00.987 に答える