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
 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 に答える
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 に答える