简体   繁体   中英

DataTemplate on DataGridColumnHeader WPF C#

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.

Code as follows:

View:

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

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);
        }
    }

And the result:

在此处输入图片说明

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.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM