条件XAML绑定(Conditional XAML

编程入门 行业动态 更新时间:2024-10-26 05:22:04
条件XAML绑定(Conditional XAML-Binding)

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

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

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

现在,我想知道如何使第二个绑定成为条件。 例如当biSendAsHtml.IsChecked等于true显示HTML,否则显示纯文本

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

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.IsChecked equals true display HTML else display Plain Text.

最满意答案

我不确定你认为sa_ddam213的答案是否优雅,这只是可怕的。 转发器,如RV1987建议,是正确的方法,但你可以更聪明。

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

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();
    }
}
 

定义转换器:

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

并使用它:

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

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

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.

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.

更多推荐

本文发布于:2023-07-31 00:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   条件   XAML   Conditional

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!