簡體   English   中英

WPF更改進度欄前景色

[英]WPF change progressbar foreground colour

我正在使用進度條,並且嘗試尋找一種方法,如果在其下面的標簽中的showError變量(布爾)為true時,將前景色設置為紅色。 我可以使用數據觸發器基於進度條的值設置前景色,如下所示,但無法捕獲showError變量中的值。 有人設法做到了嗎?

    <ProgressBar Maximum="{Binding Max}" Minimum="{Binding Min}" Name="progressBar" Value="{Binding Path=Value}" >
        <ProgressBar.Resources>
            <Style TargetType="{x:Type ProgressBar}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value}" Value="10">
                        <Setter Property="Foreground" Value="Blue"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ProgressBar.Resources>
    </ProgressBar>
    <Label Visibility="{Binding Path=ShowError, Converter={StaticResource booleanToVisibilityConverter}}" Content="Service was not available" Height="28" HorizontalAlignment="Left" Margin="408,238,0,0" Name="label2"/>

看起來ShowError是您的視圖模型的屬性。 這樣,您在ProgressBarLabel之間不需要任何關系。 只需使用以下內容:

<ProgressBar>
    <ProgressBar.Style>
        <Style TargetType="{x:Type ProgressBar}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ShowError}" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ProgressBar.Style>
</ProgressBar>

由於標簽和進度條都綁定到“視圖模型”屬性ShowError,因此您無需使標簽和進度條相互依賴。

問候Jashobanta

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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