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