WPF DataGrid行详情可见性绑定到属性(仅使用XAML)

11

我有一个显示大量对象的数据表格。这些对象具有一个属性IsDetailsExpanded,我想将数据行的DetailsVisibility属性绑定到该属性。

我的第一种方法可行,但需要一些代码后台处理(我希望摆脱它)。

我处理LoadingRow事件。

void LoadingRowHandler(object sender, DataGridRowEventArgs e)
{
    Binding b = new Binding()
    {
         Source = e.Row.DataContext,
         Path = new PropertyPath("IsExpanded"),
         Converter = (IValueConverter)Resources["BoolToVisi"],
         Mode = BindingMode.TwoWay
    };
    e.Row.SetBinding(DataGridRow.DetailsVisibilityProperty, b);
}

我认为在XAML中一定有一种类似的方法实现这个功能,但不幸的是我毫无头绪。 有任何想法或建议吗?

1个回答

22

您可以使用 Style 样式来为 DataGridRow 类型设置样式,方法如下:

<DataGrid Name="dataGrid1" Margin="12,12,0,0">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="DetailsVisibility" Value="{Binding IsExpanded, Converter={StaticResource BoolToVisi}}" />
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

没错,这就是XAML的方式。只有Property应该等于DetailsVisibility。 - Yiğit Yener
有时候我想滥用这个评论功能,在周围随意放一些表情符号。这样做会被看作是不好的吗::))))) - Yiğit Yener
1
@DarkSquirrel42 - 你的输出窗口有没有任何错误?你可能需要使用{Binding RelativeSource={RelativeSource Self}, Path=DataContext.IsExpanded, Converter={StaticResource BoolToVisi}},但我不认为会有问题。 - CodeNaked
10
咕咕咕咕咕...... 咬着桌子边缘 ... 如果DataGrid设置了"RowDetailsVisibilityMode="Collapsed"",这个方法就行不通了... - DarkSquirrel42
6
据我所知,无论RowDetailsVisibilityMode的值设置为什么,它都会覆盖这个绑定。该绑定将被忽略。 - xr280xr
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接