WPF DataGrid RowDetails可见性绑定到属性(仅限XAML)

本文关键字:仅限 XAML 属性 DataGrid RowDetails 可见性 绑定 WPF | 更新日期: 2023-09-27 18:02:50

我有一个DataGrid显示一堆对象。这些对象有一个属性IsDetailsExpanded,我想把datarow的DetailsVisibility属性绑定到那个属性。

我的第一种方法工作,但需要一些代码背后(我想摆脱)

i handle the LoadingRow event

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,但不幸的是,我没有丝毫的线索。什么好主意吗?建议吗?

WPF DataGrid RowDetails可见性绑定到属性(仅限XAML)

您可以为DataGridRow类型使用Style,如下所示:

<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>