Why MultiDataTrigger Binding failed in my CustomControl? - wpf

Here is code in Generic.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2.Controls">
<Style TargetType="{x:Type local:SampleControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SampleControl}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Property="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
<Condition Property="{Binding RelativeSource={RelativeSource Self}, Path=MyProperty}" Value="1" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="#cde8ff"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Here is code in SampleControl.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp2.Controls
{
/// <summary>
/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
///
/// Step 1a) Using this custom control in a XAML file that exists in the current project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:WpfApp2.Controls"
///
///
/// Step 1b) Using this custom control in a XAML file that exists in a different project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:WpfApp2.Controls;assembly=WpfApp2.Controls"
///
/// You will also need to add a project reference from the project where the XAML file lives
/// to this project and Rebuild to avoid compilation errors:
///
/// Right click on the target project in the Solution Explorer and
/// "Add Reference"->"Projects"->[Browse to and select this project]
///
///
/// Step 2)
/// Go ahead and use your control in the XAML file.
///
/// <MyNamespace:SampleControl/>
///
/// </summary>
public class SampleControl : Control
{
static SampleControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SampleControl), new FrameworkPropertyMetadata(typeof(SampleControl)));
}
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(SampleControl), null);
}
}
Here are all the files:
After the program ran, it reports an error:
System.Windows.Markup.XamlParseException: 'A 'Binding' cannot be set on the 'Property' property of type 'Condition'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.'
No matter I delete IsMouseOver or MyProperty condition binding, it still reports this.
What's wrong with my codes?

Condition.Property is not a DependencyProperty and cannot be a Binding.Target. You have to use the Condition.Binding property to set up a data binding:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=MyProperty}" Value="1" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="#cde8ff"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
But since you are triggering on the templated object itself, you can use a simple MultiTrigger:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="MyProperty" Value="1" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Background" Value="#cde8ff"/>
</MultiTrigger.Setters>
</MultiTrigger>

Related

Type key for style not found

I created a custom user control called FlatButton deriving from Button:
XAML:
<Button
x:Class="Program.GUI.Controls.FlatButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:GUIControls="clr-namespace:Program.GUI.Controls"
mc:Ignorable="d">
<Button.Resources>
<ResourceDictionary>
<Style x:Key="{x:Type GUIControls:FlatButton}" TargetType="{x:Type GUIControls:FlatButton}" BasedOn="{StaticResource ResourceKey={x:Type Button}}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GUIControls:FlatButton}">
<Grid x:Name="LayoutRoot" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}">
<TextBlock
x:Name="Text"
Text="{TemplateBinding Content}"
Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Foreground}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="LayoutRoot" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HoverBackground}"/>
<Setter TargetName="Text" Property="Foreground" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HoverForeground}"/>
</Trigger>
<Trigger Property="IsMouseCaptured" Value="True">
<Setter TargetName="LayoutRoot" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ClickBackground}"/>
<Setter TargetName="Text" Property="Foreground" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ClickForeground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Button.Resources>
</Button>
CS:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Program.GUI.Controls {
/// <summary>
/// Interaction logic for FlatButton.xaml
/// </summary>
public partial class FlatButton : Button {
public static readonly DependencyProperty HoverBackgroundProperty = DependencyProperty.Register(
"HoverBackground",
typeof(Brush),
typeof(FlatButton)
);
public static readonly DependencyProperty HoverForegroundProperty = DependencyProperty.Register(
"HoverForeground",
typeof(Brush),
typeof(FlatButton)
);
public static readonly DependencyProperty ClickBackgroundProperty = DependencyProperty.Register(
"ClickBackground",
typeof(Brush),
typeof(FlatButton)
);
public static readonly DependencyProperty ClickForegroundProperty = DependencyProperty.Register(
"ClickForeground",
typeof(Brush),
typeof(FlatButton)
);
public Brush HoverBackground { get; set; }
public Brush HoverForeground { get; set; }
public Brush ClickBackground { get; set; }
public Brush ClickForeground { get; set; }
static FlatButton() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(FlatButton), new FrameworkPropertyMetadata(typeof(FlatButton)));
}
public FlatButton() {
this.InitializeComponent();
}
}
}
The Problem is when I try to create an extending style the base style is not found:
MainWindow XAML:
<Style x:Key="DefaultDarkButtonThemeStyle" TargetType="{x:Type GUIControls:FlatButton}" BasedOn="{StaticResource {x:Type GUIControls:FlatButton}}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{DynamicResource ColorTextPrimaryDark}"/>
<Setter Property="HoverBackground" Value="{DynamicResource ColorPrimaryDarkLight}"/>
<Setter Property="HoverForeground" Value="{DynamicResource ColorTextPrimaryDarkLight}"/>
<Setter Property="ClickBackground" Value="{DynamicResource ColorPrimaryDarkLightLight}"/>
<Setter Property="ClickForeground" Value="{DynamicResource ColorTextPrimaryDarkLightLight}"/>
</Style>
Exception:
System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '45' and line position '47'.'
Inner Exception
Exception: Cannot find resource named 'Program.GUI.Controls.FlatButton'. Resource names are case sensitive.
If you develop a custom control like FlatButton, you should create a separate style that either resides in a theme resource dictionary (e.g. Themes/Generic.xaml) if you develop a custom control library or any other dedicated resource dictionary. Regardless of which path you choose, you can merge the corresponding resource dictionary into the application resources or any other resource dictionary on a lower scope in order to make the style accessible within that scope (apply it automatically if it is implicit or creating styles based on it, whatever).
Your partial XAML definition is the usual approach for creating UserControls, but not for custom controls. The style for FlatButton in the resources of the Button markup can only be resolved within the scope of this local resource dictionary, which means the FlatButton itself or down its visual tree. Look at the static resource lookup behavior to understand how the style is being resovled and why it fails.
The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.
The lookup process then traverses the logical tree upward to the parent element and its resource dictionary. This process continues until the root element is reached.
App resources are checked. App resources are those resources within the resource dictionary that is defined by the Application object for your WPF app.
Consequently, when your DefaultDarkButtonThemeStyle is created in your main window and tries to resolve the GUIControls:FlatButton base style, it does not find it, because it is not defined within the window resources or anywhere up the visual tree or the application resources.
However, if you simply created a theme or regular resource dictionary that contained the style and included it in the window or application resource, it would be resolved successfully.
For more information on creating custom controls, including how to set up styles and resource dictionaries, you can refer to Control authoring overview.

IValueConverter not called on RelativeSource Ancestor

I am trying to set the color of an SVG in a toggle button whenever the forecolor of the button is changed (e.g. on hover or button pressed). The interface of the SVGImage class requires to wrap the brush into a dictionary and assign it to it's CustomBrushes property. I have written a value converter for this. However, the Convert method of this converter is not called on events like mouse hovering or if the button is pressed although the text below the image changes it's color.
As I understand I have to set the relative source for the binding like this:
<ToggleButton Style="{StaticResource MyToggleButtonStyle}">
<StackPanel>
<svg:SVGImage Source="/CommonResources;component/Svg/MyIcon.svg"
CustomBrushes="{Binding
Path=Foreground,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ToggleButton}},
Converter={StaticResource BrushToDictionaryConverter}}" />
<TextBlock>Button Caption</TextBlock>
</StackPanel>
</ToggleButton>
For completeness although the problem should be in the first snippet. The style looks like this:
<Style x:Key="MyToggleButtonStyle" TargetType="ToggleButton">
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Yellow" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Foreground" Value="Green" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsChecked" Value="True"/>
<Condition Property="IsPressed" Value="True"/>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Foreground" Value="Red" />
</MultiTrigger.Setters>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and the converter like this:
/// <summary>
/// Converter to wrap a color into a dictionary.
/// </summary>
public class BrushToDictionaryConverter : IValueConverter
{
/// <summary>
/// Returns a dictionary containing a brush as value and
/// a string representation of the brush as key.
/// </summary>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SolidColorBrush scb)
return new Dictionary<string, Brush> { { scb.Color.ToString(), scb } };
else if (value is Brush b)
return new Dictionary<string, Brush> { { "custom", b } };
return new Dictionary<string, Brush> { { "empty", new SolidColorBrush(Colors.Transparent) } };
}
/// <summary>
/// Returns a brush from the dictionary assuming the provided value is a dictionary containing
/// brushes as values.
/// </summary>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Dictionary<string, Brush> dict && dict.Any())
return dict.First().Value;
return new SolidColorBrush(Colors.Transparent);
}
}
Does anyone see why the Convert method is not even called although the style is working and the button's caption changes it's color on user interaction?
I found the solution after I added diag:PresentationTraceSources.TraceLevel=High to the binding which gave me this output:
System.Windows.Data Warning: 61 : BindingExpression (hash=14000148): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=14000148): Attach to SVGImage.SVG.SVGImage.CustomBrushes (hash=57434139)
System.Windows.Data Warning: 66 : BindingExpression (hash=14000148): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 65 : BindingExpression (hash=14000148): Resolve source deferred
System.Windows.Data Warning: 79 : BindingExpression (hash=14000148): Deactivate
System.Windows.Data Warning: 63 : BindingExpression (hash=14000148): Detach
Finally, I also added the source code of the DotnetProjects.SVGImage nuget package it turned out to be a glitch in the SVGImage's OnInitialized method. At least it is working if I comment the last line of that method (which probably means that I have broken something else but for me it works.)

WPF trigger on attached property not working

i'm trying to change an image which i put as button's content through an attached property, yet for some reason the image is not changing and i don't get any exceptions or anything solid in the output.
Here is my attached property class code:
public class ImageButton
{
#region Image dependency property
/// <summary>
/// An attached dependency property which provides an
/// <see cref="ImageSource" /> for arbitrary WPF elements.
/// </summary>
public static DependencyProperty ImageProperty;
/// <summary>
/// Gets the <see cref="ImageProperty"/> for a given
/// <see cref="DependencyObject"/>, which provides an
/// <see cref="ImageSource" /> for arbitrary WPF elements.
/// </summary>
public static ImageSource GetImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(ImageProperty);
}
/// <summary>
/// Sets the attached <see cref="ImageProperty"/> for a given
/// <see cref="DependencyObject"/>, which provides an
/// <see cref="ImageSource" /> for arbitrary WPF elements.
/// </summary>
public static void SetImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(ImageProperty, value);
}
#endregion
static ImageButton()
{
//register attached dependency property
var metadataImage = new FrameworkPropertyMetadata((ImageSource)null);
ImageProperty = DependencyProperty.RegisterAttached("Image",
typeof(ImageSource),
typeof(ImageButton), metadataImage);
}
}
here the code for the style using the attached property(the image source is set to take the value):
<Style TargetType="{x:Type Button}" x:Key="MyImageButton">
<Setter Property="Background" Value="White"/>
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border
x:Name="Border"
Background="White">
<ContentPresenter>
<ContentPresenter.Content>
<Grid
x:Name="Grid"
Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image
Grid.Column="0"
Source="{Binding Path=(attachedProperties:ImageButton.Image),
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}"
Width="30"
Height="25"
Margin="5,0,2,0"
VerticalAlignment="Center"/>
<TextBlock
x:Name="TextBlock"
Grid.Column="1"
Text="{Binding Path=(attachedProperties:ImageButton.StringContent),
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}"
VerticalAlignment="Center"
TextAlignment="Center"
Margin="0,0,15,0"/>
</Grid>
</ContentPresenter.Content>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and finally the XAML code using the style and the trigger there trying to change the image source:
<Button
Grid.Row="2"
Width="30"
attachedProperties:ImageButton.Image="..\Images\Down_Arrow_Enabled_Icon_25x25.png"
Command="{Binding PriorityDownCommand}">
<Button.Style>
<Style TargetType="{x:Type Button}"
BasedOn="{StaticResource MyImageButton}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="attachedProperties:ImageButton.Image" Value="..\Images\Down_Arrow_Disabled_Icon_25x25.png"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
thanks ahead for anyone who can find the problem!!!
Please take a look at this Dependency Property Value Precedence article.
Your problem is that you are trying to override a Local Property Value from withing a Style Trigger, this cannot be done since the Local Value has a higher precedence value.
You should set the default property in style setters:
<Button
Grid.Row="2"
Width="30"
Command="{Binding PriorityDownCommand}">
<Button.Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource MyImageButton}">
<Setter Property="attachedProperties:ImageButton.Image" Value="..\Images\Down_Arrow_Enabled_Icon_25x25.png"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="attachedProperties:ImageButton.Image" Value="..\Images\Down_Arrow_Disabled_Icon_25x25.png"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>

Applying Style to child elements in Silverlight 4

In Silverlight 4 (using Expression Blend 4), how can I alter the Font size of a TextBox within the style of the Border containing it? I'm converting the style from WPF to Silverlight (fun always). Here is what I have:
<Style x:Key="Title" TargetType="Border">
<Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
<Setter Property="TextBlock.TextAlignment" Value="Center"/>
<Setter Property="TextBlock.FontSize" Value="48"/>
<Setter Property="TextBlock.Foreground" Value="{StaticResource TextForeground}"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Background" Value="{StaticResource TitleBackground}"/>
<Setter Property="Padding" Value="25,0"/>
</Style>
It is not working. It gives me the following exception in the designer:
Edits:
Ok, I know this is possible in WPF. Is it simply not possible in Silverlight (without taking on a whole theme construct as Xin suggests?)
Actually you might be able to get what you want from silverlight toolkit themes. You can find it here (Theming -> Theme Browser).
Update:
First you need to create a class that inherits from the Theme (System.Windows.Controls.Theming). I basically copied from the source code and renamed it.
/// <summary>
/// Implicitly applies the border theme to all of its descendent
/// FrameworkElements.
/// </summary>
/// <QualityBand>Preview</QualityBand>
public class BorderTheme : Theme
{
/// <summary>
/// Stores a reference to a Uri referring to the theme resource for the class.
/// </summary>
private static Uri ThemeResourceUri = new Uri("/theming;component/Theme.xaml", UriKind.Relative);
/// <summary>
/// Initializes a new instance of the ExpressionDarkTheme class.
/// </summary>
public BorderTheme()
: base(ThemeResourceUri)
{
var a = ThemeResourceUri;
}
/// <summary>
/// Gets a value indicating whether this theme is the application theme.
/// </summary>
/// <param name="app">Application instance.</param>
/// <returns>True if this theme is the application theme.</returns>
public static bool GetIsApplicationTheme(Application app)
{
return GetApplicationThemeUri(app) == ThemeResourceUri;
}
/// <summary>
/// Sets a value indicating whether this theme is the application theme.
/// </summary>
/// <param name="app">Application instance.</param>
/// <param name="value">True if this theme should be the application theme.</param>
public static void SetIsApplicationTheme(Application app, bool value)
{
SetApplicationThemeUri(app, ThemeResourceUri);
}
}
Then you just need to create a resource dictionary and name it Theme.xaml, and put all your styles inside.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Border">
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Background" Value="{StaticResource TitleBackground}"/>
<Setter Property="Padding" Value="25,0"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="FontSize" Value="48"/>
<Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
</Style>
</ResourceDictionary>
Last, wrap your Border with it!
<local:BorderTheme>
<Border>
<TextBlock Text="TextBlock"/>
</Border>
</local:BorderTheme>
That's it. You should be able to see the styles applied to your Border as well as your TextBlock. :)

Style template does not reevaluate when Data updates

I have a style template (below) that does not update when my Tag binding updates. The data itself updates and I receive no binding errors so expect everything is bound correctly it is just that the style doesn't seem to update at all. I have notifypropertychanged events in all the right places afaik so doubt this is the issue.
Thanks
<Style x:Key="CompareTemplate" TargetType="TextBlock">
<!--Setter Property="Foreground" Value="#FF760000" /-->
<Setter Property="Foreground" Value="#FFBCBCBC" />
<Style.Triggers>
<Trigger Value="True" Property="Tag">
<Setter Property="Foreground" Value="#FF007602" />
</Trigger>
<Trigger Value="False" Property="Tag">
<Setter Property="Foreground" Value="#FF760000" />
</Trigger>
</Style.Triggers>
</Style>
And I use this template like so:
<TextBlock Style="{DynamicResource CompareTemplate}" Tag="{Binding UnitComparer.CommsID, FallbackValue=True}" Text="Comms ID:" />
Tag is of type object. I think that your Viewmodel assings a bool to it. WPF is able to convert between strings and objects but seemingly not between bool and object. One solution is to use a IValueConverter to change the bool to a string:
<Window x:Class="BindToTagSpike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindToTagSpike"
Title="Window1" Height="300" Width="300">
<StackPanel>
<StackPanel.Resources>
<local:ObjectToString x:Key="ObjectToString"/>
<Style x:Key="CompareTemplate" TargetType="TextBlock">
<Style.Triggers>
<Trigger Value="True" Property="Tag">
<Setter Property="Foreground" Value="Red" />
</Trigger>
<Trigger Value="False" Property="Tag">
<Setter Property="Foreground" Value="YellowGreen" />
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<TextBlock Style="{StaticResource CompareTemplate}"
Name="TaggedTextBlock"
Tag="{Binding TagValue,
Converter={StaticResource ObjectToString}}"/>
<Button Click="Button_Click">Change Style</Button>
</StackPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace BindToTagSpike
{
public partial class Window1 : Window, INotifyPropertyChanged
{
public Window1()
{
InitializeComponent();
tagValue = false;
TaggedTextBlock.Text = "Test";
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TagValue=!TagValue;
}
private bool tagValue;
public bool TagValue
{
get { return tagValue; }
set
{
tagValue = value;
if (PropertyChanged != null)
PropertyChanged(this,new PropertyChangedEventArgs("TagValue"));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public class ObjectToString : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
Sometime when your template / style is not being applied as expected, WPF might not think the TargetType might matches the control type. Try the code below and see if that helps at all:
<Style x:Key="CompareTemplate" >
<!--Setter Property="Control.Foreground" Value="#FF760000" /-->
<Setter Property="Control.Foreground" Value="#FFBCBCBC" />
<Style.Triggers>
<Trigger Value="True" Property="Control.Tag">
<Setter Property="Control.Foreground" Value="#FF007602" />
</Trigger>
<Trigger Value="False" Property="Control.Tag">
<Setter Property="Control.Foreground" Value="#FF760000" />
</Trigger>
</Style.Triggers>
</Style>
Cheers,
Berryl

Resources