【问题标题】:WPF DataGridTemplateColumn databind to control outside?WPF DataGridTemplateColumn 数据绑定到控制外部?
【发布时间】:2011-01-20 11:41:23
【问题描述】:

我有一个放在用户控件中的控件,它公开了一个带有 DepenceyPropety(ShowCommand) 的 ICommand, 然后我有一个带有几列的数据网格(wpf 工具包),其中一个有一个删除按钮

<Custom:DataGrid Margin="0" ItemsSource="{Binding Todos}" AutoGenerateColumns="False">
    <Custom:DataGrid.Columns>
      <Custom:DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        <Custom:DataGridTemplateColumn>
            <Custom:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button CommandParameter="{Binding}"
                            Command="{Binding ElementName=ConfirmDeleteDialog, Path=ShowCommand}" 
                            Content="Delete" />
                </DataTemplate>
            </Custom:DataGridTemplateColumn.CellTemplate>
        </Custom:DataGridTemplateColumn>
    </Custom:DataGrid.Columns>
</Custom:DataGrid>
<local:ConfirmationDialog x:Name="ConfirmDeleteDialog" 
                          Title="Confirm delete!" 
                          d:LayoutOverrides="Width" 
                          Message="Are you sure that you want to delete the selected todo?" 
                          YesCommand="{Binding DeleteCommand}" />

由于我在 DataTemplate 中找不到元素,我无法结合 ElementName 和相对源,所以我将如何定义可以访问在数据模板之外声明的元素的绑定?

我试图绑定的命令位于 ConfirmationDialog.YesCommand..

【问题讨论】:

    标签: wpf data-binding


    【解决方案1】:

    好吧,我通过这种方式解决了它,我将对话框更改为从 ContentControl 继承而不是 Control 并在其中添加了一个内容持有者..

    之后我移动了对话框,使它包裹了整个控件,然后我添加了一个我可以收听的路由事件,它就像一个魅力。

    【讨论】:

      猜你喜欢
      • 2019-05-09
      • 1970-01-01
      • 2010-10-10
      • 2011-09-06
      • 2012-10-13
      • 2011-02-06
      • 2012-04-21
      • 1970-01-01
      • 2011-04-06
      相关资源
      最近更新 更多