如何将 CommandParameter 绑定到 DataTemplate 中父控件的 x:name?

pau*_*aul 1 wpf binding datatemplate

我的 View XAML 中有以下内容

<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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

命令在 ViewModel 中定义

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

我希望能够确定 2 个列表中的哪个生成了命令。我希望将 CommandParameter 传递给包含控件的 x:Name 的命令处理程序。

如何定义绑定?有没有更好的办法?

Tor*_*tad 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>
Run Code Online (Sandbox Code Playgroud)

作为旁注,您的示例几乎包含上面一行中的类似技术,您可以在其中绑定到 Command 属性。