有没有办法进行这种多重绑定:
<TextBox.IsEnabled>
<MultiBinding Converter="{StaticResource LogicConverter}">
<Binding ElementName="prog0_used" Path="IsEnabled" />
<Binding ElementName="prog0_used" Path="IsChecked" />
</MultiBinding>
</TextBox.IsEnabled>
并且put全部放在一行,如<TextBox IsEnabled="" />
?
如果是这样,我在哪里可以学习这种格式的规则?
答案 0 :(得分:16)
更好(更简单)的方法是将样式定义为可以轻松应用于任何TextBox的资源:
<Window.Resources>
<c:MyLogicConverter x:Key="LogicConverter" />
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}" x:Key="MultiBound">
<Setter Property="IsEnabled">
<Setter.Value>
<MultiBinding Converter="{StaticResource LogicConverter}">
<Binding ElementName="switch" Path="IsEnabled" />
<Binding ElementName="switch" Path="IsChecked" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel Orientation="Horizontal">
<CheckBox Name="switch" />
<TextBox Name="textBox2" Text="Test" Style="{StaticResource MultiBound}" />
</StackPanel>
答案 1 :(得分:5)
这可以通过自定义标记扩展来完成:
public class MultiBinding : System.Windows.Data.MultiBinding
{
public MultiBinding (BindingBase b1, BindingBase b2)
{
Bindings.Add(b1);
Bindings.Add(b2);
}
public MultiBinding (BindingBase b1, BindingBase b2, BindingBase b3)
{
Bindings.Add(b1);
Bindings.Add(b2);
Bindings.Add(b3);
}
// Add more constructors if you need.
}
用法:
<TextBox IsEnabled="{local:MultiBinding
{Binding IsEnabled, ElementName=prog0_used},
{Binding IsChecked, ElementName=prog0_used},
Converter={StaticResource LogicConverter}}">
答案 2 :(得分:4)
对于MultiBinding,没有简写字符串。您需要使用扩展元素语法。
答案 3 :(得分:3)
我尝试使用Discord的答案,但它没有开箱即用。为了使它工作,我添加了一个新的构造函数:
public class MultiBinding : System.Windows.Data.MultiBinding
{
public MultiBinding(BindingBase b1, BindingBase b2, object converter)
{
Bindings.Add(b1);
Bindings.Add(b2);
Converter = converter as IMultiValueConverter;
}
}
用法将如下:
<TextBox IsEnabled="{local:MultiBinding {Binding IsEnabled, ElementName=prog0_used},
{Binding IsChecked, ElementName=prog0_used},
{StaticResource LogicConverter}}">