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

时间:2011-07-20 20:16:01

标签: c# wpf xaml data-binding datagrid

我有一个显示一堆对象的DataGrid。这些对象具有属性IsDetailsExpanded,我想将DataRows 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 个答案:

答案 0 :(得分:17)

您可以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>