簡體   English   中英

純粹在XAML中基於其工作日在其Text屬性中設置textBlock前景

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM