WPF DataGrid - 在每行的末尾添加按钮

时间:2014-08-24 21:18:26

标签: c# wpf datagrid

我想在datagrid的每一行的末尾添加一个按钮。我找到了以下xaml,但它在开始时添加了按钮。有人知道如何在所有数据绑定列之后添加它吗?

这会将按钮添加到开头而不是结尾:

  <DataGrid Background ="Black" ItemsSource="{Binding PriceList}">
    <DataGrid.Columns>
      <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <Button>My button</Button>
          </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
    </DataGrid.Columns>
  </DataGrid>

非常感谢提前

4 个答案:

答案 0 :(得分:11)

您必须手动添加列。

关闭列的自动生成并按照您想要的顺序添加它们,包括列表末尾的额外列:

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

答案 1 :(得分:2)

找到一些样式的按钮控件。 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>

答案 2 :(得分:1)

如果您的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>

答案 3 :(得分:0)

您可以使用转换器按其名称选择哪一列!

我创建了这样的列:(也许它可以与其他方法一起使用)

DataGridTextColumn column = new DataGridTextColumn();
column.Binding = new Binding("ColumnName");
dataGrid.Columns.Add(column)

在您的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}" />

用列名替换ColumnName ...

您的转换器:

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