通过WPF中的MVVM模式更改按钮背景颜色

时间:2022-11-20 15:16:46

I am using MVVM light with WPF. I want to set button background color based on some specific condition through ViewModel. Kindly suggest some way to get it. Thanks

我在WPF中使用MVVM灯。我想通过ViewModel根据某些特定条件设置按钮背景颜色。请建议一些方法来获得它。谢谢

2 个解决方案

#1


22  

Using triggers:

使用触发器:

<Button>
    <Button.Style>
        <Style TargetType="Button">
            <!-- Set the default value here (if any) 
                 if you set it directly on the button that will override the trigger -->
            <Setter Property="Background" Value="LightGreen" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding SomeConditionalProperty}"
                             Value="True">
                    <Setter Property="Background" Value="Pink" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

[Regarding the note]

[关于说明]


In MVVM you can also often handle this in the view-model via get-only properties as well, e.g.

在MVVM中,您也可以经常通过get-only属性在视图模型中处理此问题,例如:

public bool SomeConditionalProperty 
{
    get { /*...*/ }
    set
    {
        //...

        OnPropertyChanged("SomeConditionalProperty");
        //Because Background is dependent on this property.
        OnPropertyChanged("Background");
    }
}
public Brush Background
{
    get
    {
        return SomeConditinalProperty ? Brushes.Pink : Brushes.LightGreen;
    }
}

Then you just bind to Background.

然后你只需绑定到背景。

#2


23  

you could bind the Background to a property on the viewmodel the trick is to use a IValueConverter to return a brush with the color your require, heres an example that converts a boolean value from the viewmodel to a color

你可以将Background绑定到viewmodel上的一个属性,诀窍是使用IValueConverter返回一个你需要的颜色的画笔,这是一个将一个布尔值从viewmodel转换为一个颜色的例子

public class BoolToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return new SolidColorBrush(Colors.Transparent);
        }

        return System.Convert.ToBoolean(value) ? 
            new SolidColorBrush(Colors.Red)
          : new SolidColorBrush(Colors.Transparent); 
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

with a binding expression like

用一个绑定表达式

    "{Binding Reviewed, Converter={StaticResource BoolToColorConverter}}"

#1


22  

Using triggers:

使用触发器:

<Button>
    <Button.Style>
        <Style TargetType="Button">
            <!-- Set the default value here (if any) 
                 if you set it directly on the button that will override the trigger -->
            <Setter Property="Background" Value="LightGreen" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding SomeConditionalProperty}"
                             Value="True">
                    <Setter Property="Background" Value="Pink" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

[Regarding the note]

[关于说明]


In MVVM you can also often handle this in the view-model via get-only properties as well, e.g.

在MVVM中,您也可以经常通过get-only属性在视图模型中处理此问题,例如:

public bool SomeConditionalProperty 
{
    get { /*...*/ }
    set
    {
        //...

        OnPropertyChanged("SomeConditionalProperty");
        //Because Background is dependent on this property.
        OnPropertyChanged("Background");
    }
}
public Brush Background
{
    get
    {
        return SomeConditinalProperty ? Brushes.Pink : Brushes.LightGreen;
    }
}

Then you just bind to Background.

然后你只需绑定到背景。

#2


23  

you could bind the Background to a property on the viewmodel the trick is to use a IValueConverter to return a brush with the color your require, heres an example that converts a boolean value from the viewmodel to a color

你可以将Background绑定到viewmodel上的一个属性,诀窍是使用IValueConverter返回一个你需要的颜色的画笔,这是一个将一个布尔值从viewmodel转换为一个颜色的例子

public class BoolToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return new SolidColorBrush(Colors.Transparent);
        }

        return System.Convert.ToBoolean(value) ? 
            new SolidColorBrush(Colors.Red)
          : new SolidColorBrush(Colors.Transparent); 
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

with a binding expression like

用一个绑定表达式

    "{Binding Reviewed, Converter={StaticResource BoolToColorConverter}}"