Question

I have a button. When it is pressed a button color should change. The color is chosen according to certain conditions( example: Ok-Grey, error - Red). It is simple task, but I can not solve it :(. what I have tried. I define the term before clicking on the button and store it in a property Tag( this button ). I did not work for the normal button. I override the template button:

 <Style x:Key="SimpleButton" TargetType="{x:Type Button}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="MinHeight" Value="23" />
        <Setter Property="MinWidth" Value="75" />
        <Setter Property="Tag" Value="1" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="Border"
                            Background="#C0C0C0"
                            BorderBrush="#404040"
                            BorderThickness="1"
                            CornerRadius="2">
                        <ContentPresenter Margin="0,6,27,6"
                                          HorizontalAlignment="Right"
                                          VerticalAlignment="Center"
                                          RecognizesAccessKey="True" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <!-- !!!!! -->
                        <Trigger Property="IsPressed" Value="true">
                           <Setter TargetName="Border" Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag, Converter={StaticResource StatusToBrushConverter}}" />
                        </Trigger>                            
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter TargetName="Border" Property="BorderBrush" Value="#202020" />
                        </Trigger>
                        <Trigger Property="IsDefaulted" Value="true">
                            <Setter TargetName="Border" Property="BorderBrush" Value="#202020" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
                            <Setter Property="Foreground" Value="#888888" />
                        </Trigger>                  
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Converter works only once. Further, any property changes Tag do not start work converter. What do you advise?

Was it helpful?

Solution

I would probably use an attached dependency property instead of the tag, something like this

<Window x:Class="test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:test"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="local:ButtonThing.Tag2" Value="2"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <ControlTemplate.Triggers>
                            <Trigger Property="local:ButtonThing.Tag2" Value="1">
                                <Setter Property="Background" Value="Red"/>
                            </Trigger>
                            <Trigger Property="local:ButtonThing.Tag2" Value="2">
                                <Setter Property="Background" Value="Blue"/>
                            </Trigger>
                        </ControlTemplate.Triggers>

                        <Grid Background="{TemplateBinding Background}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button local:ButtonThing.Tag2="1"/>
    </Grid>
</Window>

And the code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace test
{
    public class ButtonThing : DependencyObject
    {
        public static object GetTag2(DependencyObject obj)
        {
            return (object)obj.GetValue(Tag2Property);
        }
        public static void SetTag2(DependencyObject obj, object value)
        {
            obj.SetValue(Tag2Property, value);
        }
        public static readonly DependencyProperty Tag2Property = DependencyProperty.RegisterAttached("Tag2", typeof(object), typeof(ButtonThing), new PropertyMetadata(null));
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top