WPF 复选框多绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15279388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:57:20  来源:igfitidea点击:

WPF CheckBox multiBinding

c#wpfdata-binding

提问by RayOldProf

I need to bind a checkBox to TWO property and I think that i have to use multiBindings

我需要将一个复选框绑定到两个属性,我认为我必须使用 multiBindings

so far i have this, but this doesn't work.

到目前为止,我有这个,但这不起作用。

<CheckBox x:Name="FilterAll" Content="All">
 <CheckBox.IsChecked>
  <MultiBinding>
     <Binding Path="SearchEngineCompassLogView.FilterSearch.IsFilterAllEnable"
            Source="{StaticResource CompassLogView}">
     </Binding>

     <Binding Path="SearchEngineCompassLogView.FilterSearch.IsFilterVisible"
             Source="{StaticResource CoreServiceLogView}">
     </Binding>
  </MultiBinding>
 </CheckBox.IsChecked>
</CheckBox>

is this even possible with MultiBinding?

MultiBinding 甚至可以做到这一点吗?

回答by Liz

You could use MultiBinding. And as ethicallogics said, you must use a converter to do the actual logic of the parameters (whether you want to do AND, OR, whatever. You can see a little more about those here

您可以使用多绑定。正如 ethicslogics 所说,您必须使用转换器来执行参数的实际逻辑(无论您想执行 AND、OR 还是其他操作。您可以在此处查看更多有关这些内容的信息)

I'm not sure what you are trying to affect on your checkbox, but in the end it will look something like this.

我不确定您想对复选框产生什么影响,但最终它会看起来像这样。

<CheckBox.IsChecked>
   <MultiBinding Converter="{StaticResource MultiBoolConverter}">
        <Binding Path="SearchEngineCompassLogView.FilterSearch.IsFilterAllEnable" Source="{StaticResource CompassLogView}"/>
        <Binding Path="SearchEngineCompassLogView.FilterSearch.IsFilterVisible"
             Source="{StaticResource CoreServiceLogView}"/>
    </MultiBinding>
</CheckBox.IsChecked>

There is also another way to do this as well, which I sometimes find useful. It's called DataTriggers. If you've done any work with Styles and Templates then you may have seen them before. Here is an example based on your question:

还有另一种方法可以做到这一点,我有时觉得它很有用。它被称为数据触发器。如果您对样式和模板做过任何工作,那么您可能以前见过它们。这是基于您的问题的示例:

<CheckBox>
  <CheckBox.Style>
     <Style TargetType={x:Type CheckBox}>
        <Style.Triggers>
          <MultiDataTrigger>
             <MultiDataTrigger.Conditions>
                  <Condition Binding="{Binding Path="SearchEngineCompassLogView.FilterSearch.IsFilterAllEnable" Source="{StaticResource CompassLogView}" Value="True"/>
                  <Condition Binding="{Binding Path="SearchEngineCompassLogView.FilterSearch.IsFilterVisible" Source="{StaticResource CoreServiceLogView}" Value="True"/>
              </MultiDataTrigger.Conditions>
              <Setter Property="CheckBox.IsChecked" Value="True"/>
          </MultiDataTrigger>
        </Style.Triggers>
     </Style>
  </CheckBox.Style>
</CheckBox>

回答by yo chauhan

You must specify converter in MultiBinding.Multibinding

您必须在 MultiBinding 中指定转换器。多重绑定