【问题标题】:Button in every WPF DataGrid Column每个 WPF DataGrid 列中的按钮
【发布时间】:2013-01-23 00:20:34
【问题描述】:

我想为每个 wpf 数据网格列添加一个按钮。我的列是自动生成的,所以我没有 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 已填充到模型中,并且工作正常。

【问题讨论】:

    标签: wpf xaml button datagrid


    【解决方案1】:

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

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

    【讨论】:

    • 好的,但我怎样才能绑定到我的列名?它们是自动创建的,并且我的数据网格的源绑定到我的模型中的数据表
    • 试试这个:
    • 您可能需要将绑定从“User.myDataTable”更改为“User.myDataTable.DefaultView”
    • 请注意,我通常不绑定到 DataTables,所以我不熟悉这种方法。
    • 好的,但现在我的 DataGrid 显示一列带有文本框文本:Application.ViewModels.UserViewmodel,然后是按钮。如果 datagridd 被填充,则文本会被自动生成的列溢出。您对此有解决方案吗?
    【解决方案2】:
     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;                     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 2018-01-15
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      相关资源
      最近更新 更多