views:

63

answers:

2

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:" />
+1  A: 

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
    }
}
Dabblernl
Bullseye! Thanks!
Chris
+1  A: 

Hi Chris

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

Berryl
Good idea but unfortunately it didn't work - thanks though
Chris