11

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?

H.B.
  • 166,899
  • 29
  • 327
  • 400
DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31

1 Answers1

22

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>
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • Exactly. That's the xaml way. Only Property should be equal to DetailsVisibility. – Yiğit Yener Jul 20 '11 at 20:25
  • Sometimes i want to abuse this commenting feature and drop some random smileys around. Is it frowned upon: :))))) – Yiğit Yener Jul 20 '11 at 20:34
  • @Yiğit - Not sure, I haven't had any complaints. – CodeNaked Jul 20 '11 at 20:38
  • XAML hates me ... i placed that rowStyle but when i change the state of that property nothing seems to happen ... anything else a wpf newbie like me should have done? – DarkSquirrel42 Jul 20 '11 at 21:12
  • 1
    @DarkSquirrel42 - Are there any errors in your Output window? You may need to use `{Binding RelativeSource={RelativeSource Self}, Path=DataContext.IsExpanded, Converter={StaticResource BoolToVisi}}`, but I wouldn't think so. – CodeNaked Jul 21 '11 at 00:00
  • no exceptions in the output window, just modules beeing loaded, etc ... i tried what you suggested, but no luck ... creating the binding in the eventhandler works fine, so i assume i missed something trivial – DarkSquirrel42 Jul 21 '11 at 00:12
  • 10
    gnahahahahahaa ... *biting in the edge of the table* ... this can't work if the DataGrid has `RowDetailsVisibilityMode="Collapsed"` set ... – DarkSquirrel42 Jul 21 '11 at 00:27
  • 6
    As far as I can tell RowDetailsVisibilityMode overrides this binding regardless of what its value is set to. The binding is ignored. – xr280xr Jun 30 '14 at 16:14