【问题标题】:How to bind CommandParameter to x:name of parent control in DataTemplate?如何将 CommandParameter 绑定到 x:DataTemplate 中父控件的名称?
【发布时间】:2012-05-01 00:05:06
【问题描述】:

我的视图 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>

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>

Command 在 ViewModel 中定义

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

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

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

【问题讨论】:

  • 您可能想看看使用TemplateBinding 或使用RelativeBindingRelativeSourceTemplatedParent。我不认为你可以得到x:Name 属性,但如果你能回到你的内容控件,那么也许你可以使用其他属性作为一种“标签”属性。

标签: wpf binding datatemplate


【解决方案1】:

我查看了您的示例并对读取 CommandParameter 的行进行了快速编辑。完成此更改后,我在 Snoop(WPF 运行时调试器)中检查了这一点,并看到 CommandParameter 设置为您描述为要应用的所需值。

首先,你可以在这里获得Snoop:

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 属性。

【讨论】:

    【解决方案2】:

    创建自引用属性

    我讨厌 WPF。但是,我只是这样做了:将此属性添加到绑定的数据模型类中:

    public class MyDataObjectItem
    {
        //...
        public MyDataObjectItem Self { get { return this; } }
        //...
    }
    

    那么命令参数就简单了:

    CommandParameter="{Binding Self}"
    

    或者,显然这是可行的

    CommandParameter="{Binding}"
    

    https://stackoverflow.com/a/11287478/887092

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 2013-08-07
      相关资源
      最近更新 更多