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
}
}