WPF与2复选框绑定 [英] WPF Binding with 2 Checkboxes

查看:125
本文介绍了WPF与2复选框绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想2复选框绑定在一起,因此当一个被选中的,另一种是复选框被禁用。

现在我只是试图得到任何工作。绑定之一,所以,如果没有选中它,它绑定到其他元素被禁用。最后,我想这是任何检查的倒数(要只有一个或其他检查。)我知道我将不得不使用一个转换器的。该绑定器isChecked工作正常,只是没有IsEnabled属性。任何帮助将大大AP preciated。

 < GridViewColumn.CellTemplate>
     <&DataTemplate的GT;
          <电网的Horizo​​ntalAlignment =拉伸>
               <复选框名称=kioskRequiredCB=器isChecked{结合DefaultKioskAsRequired}IsEnabled ={绑定的ElementName = kioskHiddenCB,路径=器isChecked,模式=双向}/>
           < /网格和GT;
      < / DataTemplate中>
 < /GridViewColumn.CellTemplate>< GridViewColumn.CellTemplate>
     <&DataTemplate的GT;
          <电网的Horizo​​ntalAlignment =拉伸>
                <复选框名称=kioskHiddenCB=器isChecked{结合DefaultKioskAsHidden}IsEnabled ={绑定的ElementName = kioskRequiredCB,路径=器isChecked,模式=双向}/>
          < /网格和GT;
      < / DataTemplate中>
 < /GridViewColumn.CellTemplate>


解决方案

你的两个复选框被宣布里面的模板,这是单独的名称范围,所以不能看到对方的名字做一个绑定的ElementName。为了解决这个问题,你需要绑定到很常见的两个数据,这看起来你已经有:

 < GridViewColumn.CellTemplate>
     <&DataTemplate的GT;
          <电网的Horizo​​ntalAlignment =拉伸>
               <复选框名称=kioskRequiredCB=器isChecked{结合DefaultKioskAsRequired}
                         IsEnabled ={结合DefaultKioskAsHidden,转换器= {StaticResource的SomeInvertingBooleanConverter}/>
           < /网格和GT;
      < / DataTemplate中>
 < /GridViewColumn.CellTemplate>< GridViewColumn.CellTemplate>
     <&DataTemplate的GT;
          <电网的Horizo​​ntalAlignment =拉伸>
                <复选框名称=kioskHiddenCB=器isChecked{结合DefaultKioskAsHidden}
                          IsEnabled ={结合DefaultKioskAsRequired,转换器= {StaticResource的SomeInvertingBooleanConverter}}/>
          < /网格和GT;
      < / DataTemplate中>
 < /GridViewColumn.CellTemplate>

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>

解决方案

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>

这篇关于WPF与2复选框绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆