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

问题描述 投票:0回答:2

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

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

谢谢

c# wpf xaml
2个回答
2
投票

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

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

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


0
投票

现在,我想根据文本属性中的星期几来设置该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>
© www.soinside.com 2019 - 2024. All rights reserved.