【问题标题】:How to bind the Command to its "TemplatedParent"如何将命令绑定到其“TemplatedParent”
【发布时间】:2016-05-09 20:06:44
【问题描述】:

如何将按钮“myCmdButton”的命令绑定到ParameterVariableList,就像“addBtn”一样。 “addBtn”按预期工作。 我也想要我的 ParameterVariableList 类的其他按钮命令。

这是 XAML:

<DataTemplate DataType="{x:Type locale:ParameterVariableList}" >
    <StackPanel Orientation="Vertical">

        <TextBlock Text="{Binding Description}"/>
        <Button Content="add" Command="{Binding AddEntryCmd}" Name="addBtn" />

        <ListBox ItemsSource="{Binding ItemList, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Stretch" >
            <ListBox.ItemTemplate>
                <ItemContainerTemplate>
                    <StackPanel Orientation="Horizontal">

                        <TextBlock Text="{Binding}" />

                        <Button Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" Command="{Binding RemoveEntryCmd, ???}" Name="myCmdButton" >
                            <Image Source="Resources/trash_16x16.png" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center" />
                        </Button>

                    </StackPanel>
                </ItemContainerTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</DataTemplate>

我希望这会起作用,但它没有:

Command="{Binding RemoveEntryCmd, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type locale:ParameterVariableList}}}"

错误: System.Windows.Data 错误:4:找不到与引用'RelativeSource FindAncestor,AncestorType='hmi.ParameterVariableList',AncestorLevel='1''的绑定源。 BindingExpression:Path=RemoveEntryCmd;数据项=空;目标元素是 'Button' (Name='');目标属性是“Command”(输入“ICommand”)

【问题讨论】:

    标签: c# wpf xaml data-binding


    【解决方案1】:

    这行得通:

    Command="{Binding DataContext.RemoveEntryCmd, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"
    

    几乎是 Abhinav 的答案,但缺少 DataContext。

    【讨论】:

      【解决方案2】:

      这应该可以工作

      Command="{Binding RemoveEntryCmd,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBox}}}
      

      【讨论】:

      • 没有。 System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“ListBox”(名称=“”)上找不到“RemoveEntryCmd”属性。 BindingExpression:Path=RemoveEntryCmd;数据项='列表框'(名称='');目标元素是 'Button' (Name='');目标属性是“Command”(类型“ICommand”).. 也尝试过 AncestorType x:Type locale:ParamaterVariableList .. 不工作..
      • Sorry.The Ancestor Type 应该是 DataTemplate 使用的父 Type 控件。 Eg.如果Data Template的parent是datagrid那么Datagrid应该是theAncestor Type。
      • 该 DataTemplate 的父级是 ListView,但相同的错误消息:System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“ListView”上找不到“RemoveEntryCmd”属性(名称='listview_settings')'。 BindingExpression:Path=RemoveEntryCmd; DataItem='ListView'(名称='listview_settings');目标元素是 'Button' (Name='');目标属性是'Command'(类型'ICommand')..这对我来说甚至不合逻辑,因为它不是那个ListView的属性?
      • 您的 RemoveEntryCmd 是否正常工作?你有没有在模板外测试过命令。
      • 将 AddEntryCmd 替换为 RemoveEntryCmd 并执行!
      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多