Vishal Parekh
Vishal Parekh

Reputation: 168

How to add button in first row of datagrid column?

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 ?

Upvotes: 1

Views: 1657

Answers (1)

Eirik
Eirik

Reputation: 4205

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

Upvotes: 1

Related Questions