我需要绑定(bind)一个TextBox
满足两个标准:
在哪里
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/