Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a DataGrid displaying bunch of Objects. Those objects have a property IsDetailsExpanded and I want to bind the DataRows DetailsVisibility property to that property.

My first approach works but requires some code-behind (which i would like to get rid of)

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);
}

i think there has to be a way to achieve something similar in XAML but i unfortunately i have not the slightest clue. Any ideas? suggestions?

like image 825
DarkSquirrel42 Avatar asked Jul 20 '11 20:07

DarkSquirrel42


1 Answers

You can use a Style for the DataGridRow type, like so:

<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>
like image 150
CodeNaked Avatar answered Nov 11 '22 18:11

CodeNaked