How to add button in first row of datagrid column?

我的未来我决定 提交于 2021-01-28 08:08:24

问题


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 ?


回答1:


<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>

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; }
}


来源:https://stackoverflow.com/questions/9128494/how-to-add-button-in-first-row-of-datagrid-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!