0

我想为每个 wpf datagrid 列添加一个按钮。我的列是自动生成的,所以我没有 xaml 中列的定义。我如何使用列的模板来做到这一点,以便我有我的列标题和右侧的按钮。

编辑:

<DataGrid ItemsSource="{Binding User.myDataTable}" AutoGenerateColumns="True">
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Here I want my ColumnName" />
                                <Button Content="Button"/>
                            </StackPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.ColumnHeaderStyle>
    </DataGrid>

User.myDataTable 已填充到模型中,并且工作正常。

4

2 回答 2

0

您可以通过使用样式来做到这一点:

<DataGrid>
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding ColumnName}" />
                            <Button Content="Button" />
                        </StackPanel>
                    </ControlTemplate>
                 </Setter.Value>
             </Setter>
         </Style>
     </DataGrid.ColumnHeaderStyle>
 <DataGrid>
于 2013-02-07T14:12:27.823 回答
0
 use the below code:-

    DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();

    FrameworkElementFactory HeaderStackpanel = new    FrameworkElementFactory(typeof(StackPanel));
    FrameworkElementFactory btn = new FrameworkElementFactory(typeof(Button));
    // Set the property for  Button

    btn.SetValue(Button.MarginProperty, new Thickness(-50, 0, 0, 0));
    btn.AddHandler(Button.ClickEvent, new RoutedEventHandler(BtnClick));
     // Set the Text Value to the buttons

    btn.SetValue(Button.ContentProperty, strEdit);

   // Append the Edit Button

   HeaderStackpanel.AppendChild(btn);
   DataTemplate headerTemplate = new DataTemplate();
   headerTemplate.VisualTree = HeaderStackpanel;

   templateColumn.HeaderTemplate = headerTemplate;                     
于 2013-12-16T10:29:06.613 回答