aoakeson
aoakeson

Reputation: 602

WPF Binding with 2 Checkboxes

I am trying to bind 2 checkboxes together so that when one is checked, the other is checkbox is disabled.

Right now I am just trying to get anything to work. Bind one so that if it is not checked, the other elements that it is bound to is disabled. Eventually I want it to be the inverse of whatever is checked(To only have one or the other checked.) I know I will have to use a converter for that. The IsChecked binding is working properly, just not the IsEnabled property. Any help would be greatly appreciated.

<GridViewColumn.CellTemplate>
     <DataTemplate>
          <Grid HorizontalAlignment="Stretch">
               <CheckBox Name="kioskRequiredCB"  IsChecked="{Binding DefaultKioskAsRequired}" IsEnabled="{Binding ElementName=kioskHiddenCB, Path=IsChecked,Mode=TwoWay}" />
           </Grid>
      </DataTemplate>
 </GridViewColumn.CellTemplate>

<GridViewColumn.CellTemplate>
     <DataTemplate>
          <Grid HorizontalAlignment="Stretch">
                <CheckBox Name="kioskHiddenCB"  IsChecked="{Binding DefaultKioskAsHidden}" IsEnabled="{Binding ElementName=kioskRequiredCB, Path=IsChecked,Mode=TwoWay}" />
          </Grid>
      </DataTemplate>
 </GridViewColumn.CellTemplate>

Upvotes: 0

Views: 1664

Answers (1)

John Bowen
John Bowen

Reputation: 24453

Both of your Checkboxes are declared inside templates, which are separate name scopes, so can't see each other's names to do an ElementName Binding. To get around this you need to bind to data that's common to the two, which it looks like you already have:

<GridViewColumn.CellTemplate>
     <DataTemplate>
          <Grid HorizontalAlignment="Stretch">
               <CheckBox Name="kioskRequiredCB"  IsChecked="{Binding DefaultKioskAsRequired}"
                         IsEnabled="{Binding DefaultKioskAsHidden, Converter={StaticResource SomeInvertingBooleanConverter}" />
           </Grid>
      </DataTemplate>
 </GridViewColumn.CellTemplate>

<GridViewColumn.CellTemplate>
     <DataTemplate>
          <Grid HorizontalAlignment="Stretch">
                <CheckBox Name="kioskHiddenCB"  IsChecked="{Binding DefaultKioskAsHidden}"
                          IsEnabled="{Binding DefaultKioskAsRequired, Converter={StaticResource SomeInvertingBooleanConverter}}" />
          </Grid>
      </DataTemplate>
 </GridViewColumn.CellTemplate>

Upvotes: 2

Related Questions