WPF C#上DataGridColumnHeader的DataTemplate [英] DataTemplate on DataGridColumnHeader WPF C#

查看:447
本文介绍了WPF C#上DataGridColumnHeader的DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让DataTemplate应用于具有动态生成的列的datagrid中的所有列。 ItemsSource绑定到VM中的DataTable属性。除模板以外的所有其他东西都可以正常工作。现在,这只是概念的初步证明,因此数据是垃圾,但是在概念证明方面需要帮助。

I am having trouble getting a DataTemplate to apply to all columns in a datagrid with dynamically generated columns. The ItemsSource is bound to a DataTable property in the VM. Everything other than the template is working just fine. This is all just initial proof of concept right now, so data is garbage, but need help with the proof of concept.

代码如下:

视图:

<DataGrid AutGenerateColumns="true" ItemsSource={Binding xxx} etc...>
    <DataGrid.Columns>                
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <Button Content="ok"/>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
            </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

ViewModel-加载时,现在,我正在创建一个表。显然最终将被数据库调用替换。

ViewModel - On load, for now, I am creating a table. Obviously will eventually be replaced with a DB call...

[ImportingConstructor]
    public GenericQueueViewModel()
    {           
        int y = new Random().Next(20);
        TestList = createTableForDataGrid(y);
    }

    private DataTable createTableForDataGrid(int numberOfCols)
    {
        DataTable test = new DataTable();
        for (int i = 0; i < numberOfCols; i++)
        {
            DataColumn oDc = new DataColumn();                
            test.Columns.Add(oDc);
        }
        Random x = new Random();
        int y = x.Next(100);
        for (int i = 0; i <= y; i++)
        {
            DataRow oRow = test.NewRow();
            for (int j = 0; j < test.Columns.Count; j++)
            {
                oRow[j] = i.ToString() + " | " + j.ToString();
            }
            test.Rows.Add(oRow);
        }
        return test;
    }

private DataTable _testList;

    public DataTable TestList
    {
        get { return _testList; }
        set 
        { 
            _testList = value;
            OnPropertyChanged(() => TestList);
        }
    }

结果为:

这就像DataGrid是最初使用模板创建的,但是当OnPropertyChanged触发时,不会调用模板。网格可以很好地填充,但是没有标题中的按钮。

It's like the DataGrid is created initially with the template, but when the OnPropertyChanged fires, the template doesn't get invoked. The grid populates just fine, but without the buttons in the headers.

我是否需要对StaticResource进行某些操作才能使其正常工作?将样式拉出网格?我不太确定为什么它不起作用。

Do I need to do something with a StaticResource to get this to work? Pull the style out of the grid? I'm not really sure why it isn't working.

动态解决方案是强制性的。这是针对将要装载无论如何的队列的信息……可能有5列,也许有200列。所以我无法单独定义列。

A dynamic solution is mandatory. This is for a queue that will be loaded with "whatever" ... maybe 5 columns, maybe 200. So I can't define columns individually.

推荐答案

您实际上是通过这种方式创建了一个额外的列。要解决此问题,您需要创建一种样式以自定义列标题模板并设置标题模板。如下所示:

You are actually creating one extra column by this way. To fix this you need to create a style for customizing Column Header template and set Header Template. Something like below:

<Style TargetType="DataGridColumnHeader">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate>
        <Button Content="Ok"/>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

<DataGrid AutGenerateColumns="true" ItemsSource={Binding xxx} etc...>

</DataGrid>

这篇关于WPF C#上DataGridColumnHeader的DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆