[英]Why does the TextBlock.Foreground property in XAML not accept a Color?
[英]Set textBlock foreground based on the weekday in its Text property purely in XAML
我有一個textBlock,它在text屬性中包含一個日期。 現在,我想根據文本屬性的星期幾來設置該textBlock的前景色。
可以完全在XAML中完成嗎?
謝謝
不是在純XAML
,您需要創建一個實現IValueConverter
的類,然后通過在XAML
對其進行引用,可以將TextBlock
顏色綁定到date屬性,該屬性將通過轉換器轉換為Brush
。
有關ValueConverter
更多信息,請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
細分日期,則可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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.