简体   繁体   English

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

[英]WPF DataGrid RowDetails Visibility binding to a property (with XAML only)

I have a DataGrid displaying bunch of Objects.我有一个显示一堆对象的 DataGrid。 Those objects have a property IsDetailsExpanded and I want to bind the DataRows DetailsVisibility property to that property.这些对象有一个属性IsDetailsExpanded ,我想将 DataRows DetailsVisibility属性绑定到该属性。

My first approach works but requires some code-behind (which i would like to get rid of)我的第一种方法有效,但需要一些代码隐藏(我想摆脱)

i handle the LoadingRow event我处理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);
}

i think there has to be a way to achieve something similar in XAML but i unfortunately i have not the slightest clue.我认为必须有一种方法可以在 XAML 中实现类似的东西,但不幸的是我没有丝毫线索。 Any ideas?有任何想法吗? suggestions?建议?

You can use a Style for the DataGridRow type, like so:您可以为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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM