纯文本属性在XAML中基于工作日设置textBlock前景

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了纯文本属性在XAML中基于工作日设置textBlock前景相关的知识,希望对你有一定的参考价值。

我有一个textBlock,其中包含text属性中的日期。现在,我想根据文本属性中的星期几来设置该textBlock的前景色。

这可以纯粹在XAML中完成吗?

谢谢

答案

不是纯粹的XAML,你需要创建一个实现IValueConverter的类,然后通过在XAML中引用它,你可以将TextBlock颜色绑定到date属性,它将通过转换器转换为Brush

有关ValueConverter的更多信息,请查看此处:

https://www.codeproject.com/Tips/868163/%2FTips%2F868163%2FIValueConverter-Example-and-Usage-in-WPF

另一答案

现在,我想根据文本属性中的星期几来设置该textBlock的前景色

纯xaml:

<Style TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <Trigger Property="Text" Value="Monday"><!-- You will need to do this for every day of the week-->
            <Setter Property="Foreground" Value="Green"/>
        </Trigger>
    </Style.Triggers>
</Style>  

此外,如果您使用Runs分解日期,则可以为运行指定样式,如下所示:

<TextBlock>
    <Run Text="{Binding Today}"/>
    <Run Text="{Binding Today.DayOfWeek, Mode=OneWay}"/><!-- This has to be one way as the Property DayOfWeek is readonly -->
</TextBlock>  

然后在资源中使用这个:

<Style TargetType="{x:Type Run}">
    <Style.Triggers>
       <Trigger Property="Text" Value="Friday">
            <Setter Property="Foreground" Value="Green"/>
       </Trigger>
    </Style.Triggers>
</Style>

以上是关于纯文本属性在XAML中基于工作日设置textBlock前景的主要内容,如果未能解决你的问题,请参考以下文章