WPF DataGrid - 在每一行的末尾添加按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25476423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid - add button to end of every row
提问by user1229458
I'd like to add a button to the end of every row of my datagrid. I've found the following xaml but it adds the button to the start. Anyone know how I can add it after all the databound columns?
我想在数据网格的每一行的末尾添加一个按钮。我找到了以下 xaml,但它在开头添加了按钮。任何人都知道如何在所有数据绑定列之后添加它?
This adds the button to the start instead of the end:
这将按钮添加到开始而不是结束:
<DataGrid Background ="Black" ItemsSource="{Binding PriceList}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button>My button</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Many thanks in advance
提前谢谢了
回答by ChrisF
You'll have to add the columns manually.
您必须手动添加列。
Turn off the autogeneration of the columns and add them in the order you want them, including your extra column at the end of the list:
关闭自动生成列并按照您想要的顺序添加它们,包括列表末尾的额外列:
<DataGrid Background ="Black"
ItemsSource="{Binding PriceList}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<!-- Add your normal columns here -->
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button>My button</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
回答by Vijay Babu
Find the button control with some styles. The Style defines a Trigger element that changes the background property and visibility of a button based on button content/text.
找到一些样式的按钮控件。Style 定义了一个 Trigger 元素,该元素根据按钮内容/文本更改按钮的背景属性和可见性。
<DataGrid ItemsSource="{Binding}" Name="dgvProcessLists" SelectionMode="Single">\
<DataGrid.Columns><DataGridTextColumn Binding="{Binding SIZE_FP}" FontFamily="Verdana" Header="SIZE FP" IsReadOnly="True" Width="100" />
<DataGridTemplateColumn Width="140" Header="Command">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding DB_STEP_NAME}" Tag="{Binding STEP_ORDER}" Click="btnContinue_Click" >
<Button.Style>
<Style x:Name="ButtonVisibility">
<Setter Property="Button.Visibility" Value="Hidden"/>
<Style.Triggers>
<DataTrigger Binding="{Binding STATUS}" Value="Failed">
<Setter Property="Button.Visibility" Value="Visible"/>
<Setter Property="Button.Background" Value="#777777"/>
</DataTrigger>
<DataTrigger Binding="{Binding STATUS}" Value="Execute">
<Setter Property="Button.Visibility" Value="Visible"/>
<Setter Property="Button.Background" Value="AliceBlue"/>
</DataTrigger>
<DataTrigger Binding="{Binding STATUS}" Value="Re-Evaluate">
<Setter Property="Button.Background" Value="Blue"/>
</DataTrigger>
<DataTrigger Binding="{Binding STATUS}" Value="Final">
<Setter Property="Button.Visibility" Value="Visible"/>
<Setter Property="Button.Background" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</Datagrid>
回答by Maximus
If your DataGrid looks like this try following example:
如果您的 DataGrid 看起来像这样,请尝试以下示例:
<DataGrid ItemsSource="{Binding PriceList}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding }"/>
<Button>My button</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
回答by A. Morel
You can choose which column by its name with a converter!
您可以使用转换器按名称选择哪一列!
I created my columns like this: (maybe it works with another method)
我像这样创建了我的专栏:(也许它适用于另一种方法)
DataGridTextColumn column = new DataGridTextColumn();
column.Binding = new Binding("ColumnName");
dataGrid.Columns.Add(column)
In your DataTriggeryou define a different DataTemplatewhen the column has a certain name:
当列具有特定名称时,您DataTrigger可以定义不同DataTemplate的名称:
...
xmlns:tools="clr-namespace:App.Tools"
...
<ResourceDictionary>
<tools:CustomButtonConverter x:Key="CustomButtonConv" />
<DataTemplate x:Key="CustomButton">
<Button Click="HandlerClick" />
</DataTemplate>
<Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding Column, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource CustomButtonConv}}" Value="ColumnName">
<Setter Property="ContentTemplate" Value="{StaticResource CustomButton}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
...
<DataGrid x:Name="dataGrid" CellStyle="{StaticResource DataGridCellStyle}" />
Replace ColumnNameby the name of your column...
替换ColumnName为您的列名...
Your converter:
您的转换器:
namespace App.Tools
{
public class CustomButtonConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var dataGridTextColumn = value as DataGridTextColumn;
return dataGridTextColumn.SortMemberPath;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
}

