xml 将多重绑定放在 xaml 的一行上

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

putting multibinding on a single line in xaml

wpfxmlxamlbindingmultibinding

提问by Adam S

Is there a way to take this multibinding:

有没有办法采取这种多重绑定:

<TextBox.IsEnabled>
    <MultiBinding Converter="{StaticResource LogicConverter}">
        <Binding ElementName="prog0_used" Path="IsEnabled" />
        <Binding ElementName="prog0_used" Path="IsChecked" />
    </MultiBinding>
</TextBox.IsEnabled>

and put is all on one line, as in <TextBox IsEnabled="" />?

并把全部放在一行上,如<TextBox IsEnabled="" />

If so, where can I learn the rules of this formattiong?

如果是这样,我在哪里可以学习这种格式的规则?

采纳答案by hemp

A better (and simpler) approach would be to define a style as a resource which you can easily apply to any TextBox:

更好(也更简单)的方法是将样式定义为可以轻松应用于任何 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>

回答by Athari

This can be done with a custom markup extension:

这可以通过自定义标记扩展来完成:

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

Usage:

用法:

<TextBox IsEnabled="{local:MultiBinding
    {Binding IsEnabled, ElementName=prog0_used},
    {Binding IsChecked, ElementName=prog0_used},
    Converter={StaticResource LogicConverter}}">

回答by John Bowen

For MultiBinding there is no shorthand string. You need to use the expanded element syntax.

对于 MultiBinding,没有速记字符串。您需要使用扩展元素语法。

回答by Christian Myksvoll

I tried using Discord's answer, but it didn't work right out of the box. To make it work I added a new constructor:

我尝试使用 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;
    }
}

Usage will then be like this:

用法将如下所示:

    <TextBox IsEnabled="{local:MultiBinding {Binding IsEnabled, ElementName=prog0_used}, 
{Binding IsChecked, ElementName=prog0_used}, 
{StaticResource LogicConverter}}">