簡體   English   中英

顏色TextBlock取決於枚舉值

[英]Color TextBlock depending on enum value

我正在使用MvvM模型開發WPF。

我有一個包含Texblocks的視圖。 它顯示有關ID的信息(來自文檔和數據庫):

<GroupBox Grid.Row="1" Grid.Column="0" Header="ID Informations">
    <StackPanel Orientation="Vertical">
        <TextBlock Text="DataBase surname: "/>
        <TextBlock Text="{Binding Model.Db_SURNAME}" FontWeight="Bold"/>
        <TextBlock Text="Document surname: "/>
        <TextBlock Text="{Binding Model.Dc_SURNAME}" FontWeight="Bold"/>
        <TextBlock Text="DataBase forename: "/>
        <TextBlock Text="{Binding Model.Db_FORENAME}" FontWeight="Bold"/>
        <TextBlock Text="Document forename: "/>
        <TextBlock Text="{Binding Model.Dc_FORENAME}" FontWeight="Bold"/>
    </StackPanel>
</GroupBox>

我有一個包含錯誤代碼的枚舉:

[Flags]
public enum errorID
{
    OK = 1<<0,
    SURNAME_DIFF = 1 << 1,
    FORENAME_DIFF = 1 << 2
}

我的模型是這樣寫的:

private String _db_SURNAME;
public String Db_SURNAME
{
    get { return _db_SURNAME; }
    set { SetProperty(ref _db_SURNAME, value); }
}
[...]

private errorID _errorID;
public errorID ErrorID
{
    get { return _errorID; }
    set { SetProperty(ref _errorID, value); }
}

我希望當ErrorID.HasFlag(errorID.SURNAME_DIFF )時,兩個同時顯示Model.Db_SURNAMEModel.Dc_SURNAME文本塊都被Model.Dc_SURNAME成紅色。 並且也為Forname

我能怎么做 ?

使用將您的枚舉轉換為如下顏色的轉換器:

public class ErrorIdColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(!(value is errorID))
             throw new ArgumentException("value not of type errorID");
        if(!(parameteris errorID))
             throw new ArgumentException("parameter not of type errorID");

        errorID error = (errorID)value;
        errorID flag = (errorID)parameter;

        if (error.HasFlag(flag))
        {
            return Brushes.Red;
        }
        ...

        return Brushes.Black;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
   ....
}

}

然后,您可以使用轉換器將前景色綁定到您的枚舉:

<TextBlock Text="{Binding Model.Db_SURNAME}" FontWeight="Bold" Foreground={Binding Model.errorId, Converter={StaticRessource errorIDColorConverter}, ConverterParameter={StaticRessource errorID.SURNAME_DIFF}}/>

最簡單的解決方案:通過ErrorID的值綁定Foreground顏色

XAML

<TextBlock Text="{Binding Model.Db_SURNAME}" FontWeight="Bold" Foreground={Binding SurNameColor}/>
<TextBlock Text="Document surname: "/>
<TextBlock Text="{Binding Model.Dc_SURNAME}" FontWeight="Bold" Foreground={Binding SurNameColor}/>

模型

private Brush _surNameColor = Brush.Black;
public Brush SurNameColor
{
    get { return _surNameColor; }
    set { SetProperty(ref _surNameColor, value); }
}
private errorID _errorID;
public errorID ErrorID
{
    get { return _errorID; }
    set { 
        if(ErrorID.HasFlag(errorID.SURNAME_DIFF))
            SurNameColor = Brushes.Red;
        SetProperty(ref _errorID, value); }
}

我建議您在Model ErrorID屬性上使用DataTrigger

嘗試這樣的事情:

  1. enum的名稱空間添加到View (Visual Studio將幫助您使其正確)

     <UserControl [some other namespaces] xmlns:someName="clr-namespace:YourEnumNamespace;assembly=YourAssembly"> 
  2. 為您的TextBox添加樣式(您可以通過設置一次為所有樣式設置樣式,或添加x:Key為要擁有的每個TextBox顯式引用樣式):

     <UserControl.Resources> <Style TargetType="{x:Type TextBox}"> <Style.Triggers> <DataTrigger Binding="{Binding [your property to bind]}" Value="{x:Static someName:ErrorID.[value of enum]}"> <Setter Property="Foreground" Value="Green" /> </DataTrigger> </Style.Triggers> <!-- repeat data triggers for each enum value you want to check !--> </Style> </UserControl.Resources> 

或選擇轉換器,但我個人更喜歡這種方法。

如果在UserControl.Resources具有這種樣式的方法對您不起作用,因為您無法像這樣進行綁定,只需將其放在TextBox下的TextBox.Style標記中。

暫無
暫無

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

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