wpf - 多个绑定(bind)到 IsEnabled

标签 wpf textbox contentcontrol isenabled

我需要绑定(bind)一个TextBox满足两个标准:

  • IsEnabled if Text.Length > 0
  • IsEnabled if user.IsEnabled

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

    这是 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>
    

    最佳答案

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

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

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

    <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>
    

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

    关于wpf - 多个绑定(bind)到 IsEnabled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12307148/

    相关文章:

    wpf - WPF- Visual Studio中的文本框如何折叠区域,如何在文本编辑器中实现呢?

    c++ - 将文本框字符串转换为 float ?

    WPF 与自定义内容控件上的 DataContext 绑定(bind)

    c# - 如何在 openfiledialog 中检查文件类型

    c# - 通过单击更改 MySQL 中的 listBox 内容

    c# - 绑定(bind)到属性

    jQuery 相当于 'enabling' 输入

    c# - 为什么添加 ContentControl 会导致我的应用程序进入中断模式?

    wpf - ContentControl + RenderTargetBitmap + 空图像

    C# WPF ComboBox 鼠标悬停在颜色上