0

I have, I think a simple question. I have several buttons and depending on which one is clicked, need to bind data to my DataGrid - for examples if Button1 is clicked, bind fields A-D to the grid; if Button2 is clicked, bind fields E-J to the grid. I have the data binding working fine but can't seem to integrate the buttons to determine which data to bind. The same DataGrid is using no matter which button is pressing but I need to bind different data based on which button is clicked. Any thoughts?

  • Take a look at this question ... http://stackoverflow.com/q/320089/512365. The idea is that you bind your DataGrid columns to a collection of columns which you can change based on which button you click. – KornMuffin Nov 08 '16 at 03:29

1 Answers1

0
  1. Use ToggleButton instead of Button, as they expose IsChecked property.

  2. Declare a Freezable like <DiscreteObjectKeyFrame x:Key="A-D" Value="True"/> under Window.Resources or DataGrid.Resources .

  3. Define <BooleanToVisibilityConverter x:Key="BooleanToVisCnvKey"/> under Window.Resources or DataGrid.Resources.

  4. Bind Visibility of DataGridColumn to DiscreteObjectKeyFrame .Value declared in (2) above, and use a IValueConverter to convert boolean to Visibility.

    <Window.Resources>
       <DiscreteObjectKeyFrame x:Key="FlagKey" Value="False"/>
       <BooleanToVisibilityConverter x:Key="BooleanToVisCnvKey"/>
    </Window.Resources>
    
    ...
    <DataGrid>
        ...
        <DataGridTextColumn Visibility="{Binding Value, Source={StaticResource FlagKey}, Converter={StaticResource BooleanToVisCnvKey}}" ...>
        ...
    </DataGrid>
    
    ...
    <ToggleButton ... IsChecked="{Binding Value,Source={StaticResource FlagKey}, Mode=TwoWay}" />
    
AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38