我有一个显示一堆对象的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中实现类似的功能,但不幸的是我没有丝毫的线索。有任何想法吗?建议?
答案 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>