1

ViewXAMLに次のものがあります

<GroupBox Grid.Row="0" Header="Aktionen bei Prüfung Aufbau">
    <ContentControl Content="{Binding BuildUpActions}" ContentTemplate="{StaticResource FullActionListTemplate}" x:Name="BuildUp"/>
</GroupBox>

<GroupBox Grid.Row="1" Header="Aktionen bei Prüfung Abbau">
    <ContentControl Content="{Binding TearDownActions}" ContentTemplate="{StaticResource FullActionListTemplate}" x:Name="TearDown"/>
</GroupBox>

DataTemplateは別のリソースで定義されています

<DataTemplate x:Key="FullActionListTemplate">
    <DockPanel LastChildFill="True">
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="Neuer Ausgang" Style="{StaticResource ButtonRowStyle}"
                    Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext.NewFullIOCommand}" 
                    CommandParameter="{Binding **?HOW?**}"
                    />
            <more buttons here...>
        </StackPanel>
        <ContentControl Content="{Binding}" >

        </ContentControl>
    </DockPanel>
</DataTemplate>

コマンドはViewModelで定義されています

    public ICommand NewFullIOCommand
    {
        get
        {
            if (this._newFullIOCommand == null)
            {
                this._newFullIOCommand = new Mvvm.RelayCommand(parm => DoNewFullIO(parm));
            } return this._newFullIOCommand;
        }
    }

2つのリストのどちらがコマンドを生成したかを判別できるようにしたいと思います。Controlのx:Nameを含むコマンドハンドラーにCommandParameterを渡してほしい。

バインディングを定義するにはどうすればよいですか?もっと良い方法はありますか?

4

2 に答える 2

5

私はあなたのサンプルを見て、CommandParameter を読んでいる行を簡単に編集しました。この変更を行った後、Snoop (WPF ランタイム デバッガー) でこれを調べたところ、CommandParameter が、適用する値として説明した値に設定されていることがわかりました。

まず、Snoop はこちらから入手できます。

詮索

これを行うだけで、 CommandParameter を囲む ContentControl の名前に設定できました。

<DataTemplate x:Key="FullActionListTemplate">
        <DockPanel LastChildFill="True">
            <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Content="Neuer Ausgang"
                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext.NewFullIOCommand}" 
                CommandParameter="{Binding Path=Name, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}}"
                />
            </StackPanel>
            <ContentControl Content="{Binding}" >

            </ContentControl>
        </DockPanel>
    </DataTemplate>

補足として、あなたの例には、 Command プロパティにバインドする上記の行にほぼ同様の手法が含まれています。

于 2012-04-22T02:32:39.307 に答える