我有带有行标题名称的ObservableCollection。我应该将它绑定到我的dataGrid。
public ObservableCollection<string> criteriasName { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
...
//define row heading name
criteriasName = new ObservableCollection<string>();
for (int i = 0; i < numCriteria; i++)
criteriasName.Add(criteriaBundle[0].criterias[i].Title);
....
}
请你告诉我如何将其绑定到行标题?
<DataGrid Name="grCriteria" SelectionMode="Single" SelectionUnit="Cell" CellEditEnding="grCriteria_CellEditEnding">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding criteriasName}"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
</DataGrid>
答案 0 :(得分:0)
首先,您错过了ItemsSource
的{{1}}。所以像这样添加:
DataGrid
然后像这样更改<DataGrid Name="grCriteria" ItemsSource="{Binding}">
:
DataTemplate
此外,您应该将<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type DataGridRow}},
Path=Item.criteriasName}"/>
</DataTemplate>
变为DataContext = this;
并将其放在DataContext = criteriasName;
循环之后。