WPF: How to autogenerate row headers not just colu

2020-03-30 03:22发布

I have a WPF app with a DataGrid. I'm using AutoGenerateColumns="True" to fill it because the column and row count ain't always the same.

The problem is that it automatically generates only Column headers not the Row ones. So I decided to do what they did here: http://tech.pro/tutorial/1055/wpf-datagrid-tutorial-row-headers but I can't put the AutoGenerateColumns="False" since I'm binding there a DataTable from DataSet which is not always same size - the headers don't always contain the same texts.

With the code from link above I have both column and row headers(this one is set by manually binding it in the xaml code) but I still got there the first(headers) column too. I can't erase the first column - exception will go out.

Any hints how to get row headers without setting them manually as in the link? OR any ideas how to delete the first column which I'm using as row headers?

P.S.: I know people were asking about something like this here already, but I didn't found any question with the same problem like me and with an answer that would help me :|

标签: c# wpf header row
1条回答
戒情不戒烟
2楼-- · 2020-03-30 03:53

you can create a style in code :

        Style rowHeaderStyle=new System.Windows.Style(typeof(DataGridRowHeader));
        rowHeaderStyle.Setters.Add(new Setter(DataGridRowHeader.ContentProperty,new Binding("SomeField")));
        simpleDataGrid.RowHeaderStyle = rowHeaderStyle;

and remove this field on AutoGeneratingColumn Event :

    private void simpleDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (e.PropertyName == "SomeProperty")
            e.Cancel = true;
    }
查看更多
登录 后发表回答