DataGrid 行标题 WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35288097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGrid Row Header WPF
提问by JoelH
I have a populated DataTable (xDataTable) with 7 defined columns - the first column I want to use as my RowHeader.
我有一个包含 7 个定义列的填充数据表 (xDataTable) - 我想用作我的 RowHeader 的第一列。
I also have a DataGrid:
我也有一个 DataGrid:
<DataGrid x:Name="DataGridX" ItemsSource="{Binding}" Grid.Row="0"
CanUserAddRows="False" SelectionUnit="Cell" />
I then set the DataContext of my DataGrid:
然后我设置我的 DataGrid 的 DataContext:
DataGridX.DataContext = xDataTable;
This all works - but how can I set the first column of my DataGrid as a RowHeader?
这一切都有效 - 但是如何将 DataGrid 的第一列设置为 RowHeader?
采纳答案by Kylo Ren
Use the below style (usual case):
使用以下样式(通常情况):
<DataGrid.RowHeaderStyle>
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},Path=Columns[0].Header,Mode=OneTime}" />
</Style>
</DataGrid.RowHeaderStyle>
If we want separate RowHeader for separate Row then use:
如果我们想要单独的 RowHeader 单独的 Row 然后使用:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="IsSelected" Value="{Binding IsRowSelected}" />
<Setter Property="Header" Value="{Binding Content}" />
</Style>
</DataGrid.RowStyle>
Just change the above binding as required.
只需根据需要更改上述绑定。
If the first column is:
如果第一列是:
<DataGridTextColumn Binding="{Binding Content}" Header="Content"/>
then remove this column and use this binding for the Header.
然后删除此列并将此绑定用于标题。
回答by StepUp
you can set any headers whatever you want. Just add your columns like that:
你可以设置任何你想要的标题。只需添加您的列:
DataTable.Columns.Add("I am a Column Header!:)");
Let' see MVVM example:
让我们看看 MVVM 示例:
public class YourViewModel : ViewModelBase
{
public YourViewModel()
{
PopulateDataTable();
}
private void PopulateDataTable()
{
var _ds = new DataSet("Test");
employeeDataTable = new DataTable();
employeeDataTable = _ds.Tables.Add("DT");
for (int i = 0; i < 20; i++)
{
//you can set any Header in the following line
employeeDataTable.Columns.Add(i.ToString());
}
for (int i = 0; i < 10; i++)
{
var theRow = employeeDataTable.NewRow();
for (int j = 0; j < 20; j++)
{
theRow[j] = "a";
}
employeeDataTable.Rows.Add(theRow);
}
}
private DataTable employeeDataTable;
public DataTable EmployeeDataTable
{
get { return employeeDataTable; }
set
{
employeeDataTable = value;
OnPropertyChanged("EmployeeDataTable");
}
}
}
Your XAML:
您的 XAML:
<DataGrid ItemsSource="{Binding EmployeeDataTable}" />
Update:
更新:
Let's see code-behind example:
让我们看看代码隐藏示例:
Your XAML:
您的 XAML:
<DataGrid Name="dataGrid"/>
Your code-behind:
您的代码隐藏:
//constructor of the Window
public MainWindow()
{
InitializeComponent();
PopulateDataGrid();
}
DataTable employeeDataTable = new DataTable();
private void PopulateDataGrid()
{
var _ds = new DataSet("Test");
employeeDataTable = _ds.Tables.Add("DT");
for (int i = 0; i < 10; i++)//create columns
{
employeeDataTable.Columns.Add("I am a column!:)");
}
for (int i = 0; i < 50; i++)//fill data to rows
{
var theRow = employeeDataTable.NewRow();
for (int j = 0; j < 10; j++)
{
if (j % 2 == 0)
theRow[j] = "a";
else
theRow[j] = "b";
}
employeeDataTable.Rows.Add(theRow);
}
dataGrid.ItemsSource = employeeDataTable.AsDataView();
}

