代码之家  ›  专栏  ›  技术社区  ›  Peter

WPF DataGridTemplateColumn数据绑定到外部控件?

  •  0
  • Peter  · 技术社区  · 15 年前

    我有一个放置在用户控件中的控件,它公开了一个具有依赖属性(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}" />
    

    就像我在一个数据模板中一样,它找不到元素,而且我不能组合elementname和相对源,那么我如何定义一个可以访问在数据模板外部声明的元素的绑定呢?

    我尝试绑定到的命令位于confirmationdialog.yescommand.上。

    1 回复  |  直到 15 年前
        1
  •  0
  •   Peter    15 年前

    我用这种方法解决了这个问题,我把对话框改为从ContentControl继承而不是从Control继承,并在其中添加了一个内容持有者。

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