【问题标题】:How can I add a template for datagrid's NewItemPlaceholder row?如何为 datagrid NewItemPlaceholder 行添加模板?
【发布时间】:2013-09-20 11:27:50
【问题描述】:

我已经使用 MVVM 设计模式在我的 WPF 应用程序中实现了一个数据网格。 数据网格的每一行都有一个组合框和一个基于组合框选择的另一个控件,一切正常。
问题在于 NewItemPlaceholder(使我能够将新对象添加到 ObservableCollection 的行)默认显示 DataGrid.NewItemPlaceholder。
正如我读过的here 一样,问题是我必须为该行创建一个数据模板。并且在该链接中描述了如何以编程方式执行此操作。
有没有办法直接在 XAML 中执行此操作?
提前谢谢你。

【问题讨论】:

    标签: c# wpf xaml mvvm datagrid


    【解决方案1】:

    试试这个:

        <DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <buttons:CloseButton x:Name="CloseButton"/>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
                                      AncestorType={x:Type DataGridRow}}, 
                                      Path=IsNewItem}" Value="True">
                            <Setter TargetName="CloseButton" Property="Visibility" Value="Collapsed"></Setter>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </DataGrid.RowHeaderTemplate>
    

    我使用触发器来隐藏按钮,仅在“新项目”行上。稍微聪明一点,我应该能够更换整个模板,例如使用内容演示器并更改其模板或类似的东西。

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 2010-12-24
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多