【问题标题】:WPF Binding : Add Button to Unbound Field in gridWPF绑定:将按钮添加到网格中的未绑定字段
【发布时间】:2014-01-10 11:13:20
【问题描述】:

我正在尝试向 XamDataPresenter 中的未绑定字段添加一个按钮。

这里是按钮模板:

        <Style x:Key="CancelButtonTemplate" TargetType="{x:Type igDP:CellValuePresenter}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
                        <Button x:Name="CancelButton" Content="Cancel" Command="{Binding CancelButtonCommand}" Width="80" Height="20" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

这是 XamDataPresenter 中的未绑定字段:

                    <igDP:UnboundField Name="CancelOrder" Label="Cancel Order" Column="11">
                        <igDP:UnboundField.Settings>
                            <igDP:FieldSettings CellValuePresenterStyle="{StaticResource CancelButtonTemplate}" CellHeight="12" CellWidth="50">
                            </igDP:FieldSettings>
                        </igDP:UnboundField.Settings>
                    </igDP:UnboundField>                       
                </igDP:FieldLayout.Fields>   

按钮绑定到的“CancelButtonCommand”是视图模型中的公共属性,我已经验证它可以与 XamDataPresenter 外部的按钮一起使用并且没有模板。

按钮显示在网格中,但当我按下它时没有任何反应。

我做错了什么?

【问题讨论】:

  • 绑定有问题吗?如何纠正?
  • 您尝试过我下面的建议了吗?像我的回答一样修改你的 XAML,看看它是否有效,它应该基本上告诉命令要采取什么行动。
  • 感谢您的回复,问题仅是由于绑定造成的。我更改了路径并且它起作用了:

标签: wpf binding xamdatagrid


【解决方案1】:

我相信您需要在样式中添加一些绑定。试试改成这样:

<Button Command="{Binding DataContext.CancelButtonCommand, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}"
                                CommandParameter="{Binding}">

您使用的是 XamDataPresenter 还是 XamDataGrid?如果是演示者,请将上面的 x:Type 更改为 XamDataPresenter 而不是 XamDataGrid

另外:如果我记得的话,我必须添加 CommandParameter 以便命令知道要对哪一行进行操作,并且我需要在该方法中使用 SelectedItem。否则,每个按钮的行为都会完全相同。在我的例子中,每个按钮都应该对它所在行的对象做一些事情。

【讨论】:

  • 感谢您提及 selecteditem ..但在我的情况下,每个项目都会删除 selecteditem,因此它可以按预期工作
【解决方案2】:

使用按钮如下(元素绑定):

 <Button x:Name="CancelButton" Content="Cancel" Command="{Binding  ElementName=theotherbuttonworkingthiscommand,Path=DataContext.CancelButtonCommand}" Width="80" Height="20" />

或使用相对源绑定

<Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type window}},Path=DataContext.CancelButtonCommand}">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    相关资源
    最近更新 更多