简体   繁体   中英

WPF, setting background color with trigger

I have a customcontrol which render a textbox. I've also a style that set the color of the background based on some conditions as follows:

<Style x:Key="ArtParamStyle" TargetType="av:DC_Base">
    <Setter Property="Background" Value="{StaticResource EditableAreaBrush}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Info.Upd.IsAutoCalc}" Value="True">
            <Setter Property="Background" Value="Red" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=Forced}" Value="True">
            <Setter Property="Background" Value="LightGreen" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Initially as the value of my textbox is autocalculated, the background is correctly red. If I also set the Forced as true (by ticking a chebckbox) I've a weird result, the border of textbox is lightgreen but background not.

It seems to be a strange color, a combination of red and lightgreen. As test, if I set the "IsAutoCalc" color as Transparent, the trigger works correctly. How can I solve this?

your code seems to be correct. But I provide you my sample:

XAML:

<Window.Resources>
    <Style x:Key="ArtParamStyle" TargetType="TextBox">
        <Setter Property="Background" Value="Yellow" />
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding Bool1}" Value="True"/>
                    <Condition Binding="{Binding Bool2}" Value="False"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="Background" Value="Red" />
                <Setter Property="BorderBrush" Value="Red" />
            </MultiDataTrigger>
            <DataTrigger Binding="{Binding Bool2}" Value="True">
                <Setter Property="Background" Value="LightGreen" />
                <Setter Property="BorderBrush" Value="LightGreen" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBox Style="{StaticResource ArtParamStyle}" Height="50" Margin="4"/>
        <CheckBox IsChecked="{Binding Bool1}"/>
        <CheckBox IsChecked="{Binding Bool2}"/>
    </StackPanel>
</Grid>

In this case I used Multidatatrigger to set red background when Bool2(your forced) is not checked.

MainWindow.cs:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChaged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    private bool bool1;

    public bool Bool1
    {
        get { return bool1; }
        set { bool1 = value; RaisePropertyChaged("Bool1"); }
    }

    private bool bool2;

    public bool Bool2
    {
        get { return bool2; }
        set { bool2 = value; RaisePropertyChaged("Bool2"); }
    }

    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;
    }
}

Probably your problem is related to your custom control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM