如何在datagrid列的第一行添加按钮? [英] How to add button in first row of datagrid column?

查看:80
本文介绍了如何在datagrid列的第一行添加按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发一个wpf应用程序.我正在使用wpf工具包中的datagrid.我通过数据库中的提供程序项源绑定网格.它工作正常.现在我想在某列的第一行中添加按钮,所以有什么方法可以添加按钮?

Hello I am developing one wpf application. I am using datagrid from wpf toolkit. I am binding grid by provider item source from database. it works fine. Now i want to add button in first row in some column, so is there any way to add button ?

推荐答案

<DataGrid Name="dgtest">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button x:Name="button" Content="click me" Visibility="Collapsed" />
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=ShowButton}" Value="True">
                                <Setter TargetName="button" Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </DataTemplate.Triggers>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

这将在项目的ShowButton值设置为"True"的行上显示按钮.

This will display buttons on rows where the items has the value ShowButton set to "True".

下面是一些用一些对象填充列表的代码:

Here's some code to populate the list with some objects:

public Window2()
{
    InitializeComponent();

    ObservableCollection<test> collection = new ObservableCollection<test>();
    collection.Add(new test { ShowButton = "True" });
    collection.Add(new test { ShowButton = "False" });
    collection.Add(new test { ShowButton = "True" });

    dgtest.ItemsSource = collection;
}

public class test
{
    public string ShowButton { get; set; }
}

这篇关于如何在datagrid列的第一行添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆