使用代码隐藏的"仅添加"按钮动态生成的 WPF 数据网格列的标题



我有一个wpf数据网格,它的列在代码中动态生成,我需要在文本右侧的每个列的标题中插入小按钮,以在弹出对话框中实现自定义(复杂)过滤。

我很难弄清楚如何仅使用代码隐藏将按钮插入数据网格列标题。

这是我开始走的路线(评论了一下),但它不起作用:

private static DataGridTextColumn GetTextColumn(string ColumnName, string FormatString, bool AlignRight)
       {
           DataGridTextColumn c = new DataGridTextColumn();
           c.Header = Test.Common.UIBizObjectCache.LocalizedText.GetLocalizedText(ColumnName);
           c.Binding = new System.Windows.Data.Binding(ColumnName);
           if (!string.IsNullOrWhiteSpace(FormatString))
               c.Binding.StringFormat = FormatString;
           if (AlignRight)
           {
               Style cellRightAlignedStyle = new Style(typeof(DataGridCell));
               cellRightAlignedStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Right));
               c.CellStyle = cellRightAlignedStyle;
           }
           //var buttonTemplate = new FrameworkElementFactory(typeof(Button));
           //buttonTemplate.Text = "X";
           //buttonTemplate.AddHandler(
           //                   Button.ClickEvent,
           //                   new RoutedEventHandler((o, e) => HandleColumnHeaderButtonClick(o, e))
           //               );
           //c.HeaderTemplate=new DataTemplate(){VisualTree = buttonTemplate};
           return c;
       }

我得到一个invalidoperationexception"ContentPresenter"类型必须实现要在FrameworkElementFactory AppendChild中使用的IAddChild。"

很明显我做错了如有任何帮助,我们将不胜感激。

您需要使用模板吗?如果不使用正常的Header属性:

string colProperty = "Name";
DataGridTextColumn col = new DataGridTextColumn();
col.Binding = new Binding(colProperty);
var spHeader = new StackPanel() { Orientation = Orientation.Horizontal };
spHeader.Children.Add(new TextBlock(new Run(colProperty)));
var button = new Button();
button.Click += Button_Filter_Click;
button.Content = "Filter";
spHeader.Children.Add(button);
col.Header = spHeader;
dataGrid.Columns.Add(col);

要创建带有图像按钮的列标题,可以在xaml:中执行此操作

<Window.Resources>
        <BitmapImage x:Key="Img" UriSource="/Img/yourImage.png"/>
</Window.Resources>
        <Datagrid Name="yourDatagrid">
           <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button x:Name="Btn" Click="Btn_Click" >
                                <DockPanel>
                                    <Image Source="{StaticResource ResourceKey=Img}" Height="16" Width="16"/>
                                </DockPanel>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
         </Datagrid>

或者你可以在代码后面自动生成这个:

Xaml:

<Window.Resources>
    <BitmapImage x:Key="Img" UriSource="/Img/yourImage.png"/>
</Window.Resources>

C#:

Datagrid yourDatagrid = new Datagrid();
DataGridTemplateColumn colBtn = new DataGridTemplateColumn();
DataTemplate DttBtn = new DataTemplate();
FrameworkElementFactory btn = new FrameworkElementFactory(typeof(Button));
FrameworkElementFactory panel = new FrameworkElementFactory(typeof(DockPanel));
FrameworkElementFactory img = new FrameworkElementFactory(typeof(Image));
        img.SetValue(Image.SourceProperty, (BitmapImage)FindResource("Img"));
        img.SetValue(Image.HeightProperty, Convert.ToDouble(16));
        img.SetValue(Image.WidthProperty, Convert.ToDouble(16));
        panel.AppendChild(img);
        btn.AppendChild(panel);
        btn.AddHandler(Button.ClickEvent, new RoutedEventHandler(Btn_Click));
        DttBtn.VisualTree = btn;
        colBtn.CellTemplate = DttBtn;
        yourDatagrid.Columns.Add(colBtn);

最新更新