wpf 多重绑定到 IsEnable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12307148/
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 05:14:55  来源:igfitidea点击:

multiple binding to IsEnable

wpftextboxcontentcontrolisenabled

提问by Keith Nelson

I need to bind a TextBoxthat meets two criteria:

我需要绑定一个TextBox满足两个条件的:

  • IsEnabled if Text.Length > 0
  • IsEnabled if user.IsEnabled
  • 如果 Text.Length > 0,则为 IsEnabled
  • IsEnabled 如果 user.IsEnabled

Where user.IsEnabledis pulled from a data source. I was wondering if anyone had a easy method for doing this.

user.IsEnabled从数据源中提取出来。我想知道是否有人有一个简单的方法来做到这一点。

Here is the XAML:

这是 XAML:

<ContentControl IsEnabled="{Binding Path=Enabled, Source={StaticResource UserInfo}}"> 
    <TextBox DataContext="{DynamicResource UserInfo}" Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource LengthToBool}}"/> 
</ContentControl>

采纳答案by rrhartjr

Since you only need a logical OR, you just need two Triggers to your each of the properties.

由于您只需要一个逻辑OR,因此您只需要为每个属性设置两个触发器。

Try this XAML:

试试这个 XAML:

<StackPanel>
        <StackPanel.Resources>
            <Style TargetType="{x:Type Button}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=InputText, Path=Text}" Value="" >
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=MyIsEnabled}" Value="False" >
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Resources>
        <StackPanel Orientation="Horizontal">
            <Label>MyIsEnabled</Label>
            <CheckBox IsChecked="{Binding Path=MyIsEnabled}" />
        </StackPanel>
        <TextBox Name="InputText">A block of text.</TextBox>
        <Button Name="TheButton" Content="A big button.">     
        </Button>
    </StackPanel>

I set DataContextto the Windowclass which has a DependencyPropertycalled MyIsEnabled. Obviously you would have to modify for your particular DataContext.

我设置DataContextWindow具有DependencyProperty称为MyIsEnabled. 显然,您必须针对您的特定DataContext.

Here is the relevant code-behind:

这是相关的代码隐藏:

public bool MyIsEnabled
{
    get { return (bool)GetValue(IsEnabledProperty); }
    set { SetValue(IsEnabledProperty, value); }
}

public static readonly DependencyProperty MyIsEnabledProperty =
    DependencyProperty.Register("MyIsEnabled", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true));


public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
}

Hope that helps!

希望有帮助!

回答by Pavel Voronin

As GazTheDestroyer said you can use MultiBinding.

正如 GazTheDestroyer 所说,您可以使用 MultiBinding。

You can also acomplish this with XAML-only solution using MultiDataTrigger

您还可以使用 MultiDataTrigger 使用仅 XAML 的解决方案完成此操作

But you should switch the conditions cause triggers support only equality

但是你应该切换条件因为触发器只支持相等

<Style.Triggers>  
  <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text.Length}" Value="0" />
          <Condition Binding="{Binding Source=... Path=IsEnabled}" Value="False" />
        </MultiDataTrigger.Conditions>
        <Setter Property="IsEnabled" Value="False" />
      </MultiDataTrigger>  
</Style.Triggers>

If one of the condition is not met the value be set to its default or value from the style. But do not set local value as it overrides style's and trigger's values.

如果不满足其中一个条件,则将该值设置为其默认值或样式中的值。但不要设置本地值,因为它会覆盖样式和触发器的值。

回答by GazTheDestroyer

Bind IsEnabledusing a MultiBinding

IsEnabled使用MultiBinding绑定