这里很简单。 我创建了一个datagrid,它有一组行。我想隐藏特定的行 基于加载行后的特定逻辑?
任何想法?
答案 0 :(得分:2)
在一个Row Loaded事件上,即LoadingRow,所以在每个行加载时你得到DataGridRow,你有datacontext。让我们说Person(id,name)
这就是你可以玩的方式..
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
if(e.Row != null)
{
var row = e.Row.DataContext;
var person = row as Person;
if (person != null && person.Id == 2)
{
(e.Row as DataGridRow).IsEnabled = false;
}
if (person != null && person.Id == 1)
{
(e.Row as DataGridRow).Visibility = Visibility.Collapsed;
}
}
}
答案 1 :(得分:0)
代码需要调整,但我一直在寻找解决方案,并认为值得发布。
Private Sub gridComments_LoadingRow(sender As Object, e As DataGridRowEventArgs) Handles gridComments.LoadingRow
Dim row As DataGridRow = e.Row
For Each col As DataGridColumn In gridComments.Columns
Dim g1 As FrameworkElement = col.GetCellContent(e.Row)
Dim c As UIElement = g1.FindName("ChildElementName")
c.Opacity = 0 'Change the desired properties here
Next
End Sub
private void gridComments_LoadingRow(object sender, DataGridRowEventArgs e)
{
DataGridRow row = e.Row;
foreach (DataGridColumn col in gridComments.Columns) {
FrameworkElement g1 = col.GetCellContent(e.Row);
UIElement c = g1.FindName("ChildElementName");
c.Opacity = 0;
//Change the desired properties here
}
}
有点晚了,但这对我有用。