wpf 条件 XAML 绑定

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

Conditional XAML-Binding

wpfxamlbindingconditional-statements

提问by SeToY

I'd like to create a window title similar to Microsoft Outlook's one.

我想创建一个类似于 Microsoft Outlook 的窗口标题。

For that, I've created the following binding:

为此,我创建了以下绑定:

<MultiBinding StringFormat="{}{0} - Message ({1})}">
    <Binding ElementName="txtSubject" Path="Text" />
    <Binding ElementName="biSendAsHtml">****</Binding>
</MultiBinding>

Now I'd like to know how I can make the second binding conditional. Such as when biSendAsHtml.IsCheckedequals truedisplay HTMLelse display Plain Text.

现在我想知道如何使第二个绑定有条件。比如当biSendAsHtml.IsCheckedequalstrue显示HTMLelse 显示纯文本

回答by Rohit Vats

Create a IValueConverterand use it in your second binding -

创建一个IValueConverter并在您的第二个绑定中使用它 -

public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
                            System.Globalization.CultureInfo culture)
    {
        return (bool)value ? "HTML" : "Your Text";
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
                               System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Here goes your XAML -

这是你的 XAML -

<MultiBinding StringFormat="{}{0} - Message ({1})}">
    <Binding ElementName="txtSubject" Path="Text" />
    <Binding ElementName="biSendAsHtml" Path="IsChecked"
             Converter="{StaticResource Myconverter}"/>
</MultiBinding>

回答by Duncan Matheson

I'm not sure how you think sa_ddam213's answer is elegant, it's just scary. The converter, like RV1987 suggested, is the correct approach, but you can be a lot smarter.

我不确定你认为 sa_ddam213 的答案是优雅的,它只是可怕。转换器,就像 RV1987 建议的那样,是正确的方法,但你可以更聪明。

Create a converter which takes a bool and converts it into options defined in the Converter definition.

创建一个转换器,它接受一个布尔值并将其转换为转换器定义中定义的选项。

public class BoolToObjectConverter : IValueConverter
{
    public object TrueValue { get; set; }
    public object FalseValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Convert.ToBoolean(value) ? TrueValue : FalseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Define the Converter:

定义转换器:

<local:BoolToObjectConverter x:Key="SendAsHtmlBoolToTextConverter"
                             TrueValue="HTML"
                             FalseValue="Plain Text"/>

And use it:

并使用它:

<MultiBinding StringFormat="{}{0} - Message ({1})}">
    <Binding ElementName="txtSubject" Path="Text" />
    <Binding ElementName="biSendAsHtml" Path="IsChecked"
             Converter="{StaticResource SendAsHtmlBoolToTextConverter}"/>
</MultiBinding>

If you want you could even make TrueValue and FalseValue DependencyProperties to support Binding.

如果您愿意,您甚至可以使 TrueValue 和 FalseValue DependencyProperties 支持绑定。