1

I have a DataTemplate that is used by a listbox:

<local:BooleanToFontColorConverter x:Key="boolToFontColor" />
<DataTemplate x:Key="ListBox_DataTemplateSpeakStatus">
    <Label Width="Auto">
            <TextBlock Name="MY_TextBlock" Text="Hello!" Foreground="{Binding Path=MY_COLOR, Converter={StaticResource boolToFontColor}}" />
    </Label>
</DataTemplate>

MY_COLOR is the following bit of code:

    public class Packet_Class : INotifyPropertyChanged
    {
        private bool _my_color = false;
        public bool MY_COLOR { get { return _my_color; } 
                                       set { _my_color = value; RaisePropertyChanged("MY_COLOR"); } }
    }

and then when appropriate I set the property, which I think would fire the RaisePropertyChanged function

    myPacketClass.MY_COLOR = true;

while boolToFontColor is "trying" to use this bit:

    public class BooleanToFontColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                  CultureInfo culture)
    {
        if (value is Boolean)
        {
            return ((bool)value) ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Black);
        }

        return new SolidColorBrush(Colors.Black);
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

When I change the value of MY_COLOR from true to false, or vice versa, I see no visible changes in my text foreground color during runtime. Is anyone able to give advice as to where I am going wrong? Much appreciated and thank you in advance.

EDIT:

Some additional information to attempt to provide more clarity. I am using my DataTemplate in a ListBox like this:

<ListBox x:Name="MyUserList" ItemTemplate="{StaticResource ListBox_DataTemplateSpeakStatus}"  SelectionMode="Extended" />

And in my WPF Window element I set my local namespace to the namespace that my mainwindow.xaml.cs is encapsulated in:

xmlns:local ="clr-namespace:My_NameSpace"
mherr
  • 348
  • 1
  • 7
  • 25
  • Are you raising PropertyChanged when MY_COLOR is set? – Dan Busha Jun 27 '12 at 20:19
  • I was not, I am rather new to this stuff :) I edited my original post to something that I think makes sense after researching the RaisedPropertyChanged function. It is still not working as I would think..in fact it never breaks into my converter function. Thank you for your response/suggestion. – mherr Jun 27 '12 at 21:02
  • What is `myPacketClass.PacketHandRaised = true;` for? Do you mean `MY_COLOR = true`? – Dan Busha Jun 27 '12 at 23:34
  • Yes, I mean myPacketClass.MY_COLOR = true :) Sorry, trying to dumb down my examples but that part fell through the cracks. – mherr Jun 28 '12 at 13:12

1 Answers1

3

the RaisePropertyChanged method should raise the PropertyChanged event define in the interface and look like:

public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged (string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

the converter:

public class BooleanToFontColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
              CultureInfo culture)
    {
        if (value is Boolean)
        {
            return ((bool)value) ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Black);
        }

        return new SolidColorBrush(Colors.Black);
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

You have to use a SolidColorBrush to make it work.

It works on my environment, let me know if you encounter any trouble.

astreal
  • 3,383
  • 21
  • 34
  • Thank you for your response. I did change the converter to use SolidColorBrush and have edited my initial post to reflect that. It is still not working for me. While debugging, I have set breakpoints just about everywhere. It breaks in the property during the set, it breaks in the RaisePropertyChanged function, but it never breaks inside of the Converter. Consequently, the font color never changes to red while the value is 'true'. Does this point to my potential problem perhaps? Thanks again for your insight. – mherr Jun 28 '12 at 13:38
  • I have re-tested and all was working fine.. I tried to be as close as your code.. Give me more info: in which context do you use your data template (I've tried using a ListBox), Are you sure the Binding to the template is ok? and the binding to the data (between a list of Packet_Class and the listBox in my case)? how are you settinhg the namespace (local)? Have you reference your ResourceDictionary in the app.xaml or the page?? – astreal Jun 28 '12 at 14:24
  • Upon further debugging, it seems that my line "if (PropertyChanged != null)" fails. So my public event is null ;/ – mherr Jun 28 '12 at 14:25
  • that's weird, this means that no elements is listening to/handles your PropertyChanged event. How looks your RaisePropertyChanged. It hapens normally when the call to that method is not ok : [link]http://stackoverflow.com/questions/1512627/propertychanged-event-always-null[/link] – astreal Jun 28 '12 at 14:31
  • one other possiblity is your setting the datacontext wrongly. – astreal Jun 28 '12 at 14:34
  • Thank you for your extensive help. From your link, I tried declaring my public event like this: public event PropertyChangedEventHandler PropertyChanged = delegate { }; Doing this makes my PropertyChanged no longer null, but still does not change my font color. The breakpoint inside of the converter still do not trigger. I will now try to play with the datacontext, as I am not sure I am setting that at all just yet. Thank you again! – mherr Jun 28 '12 at 14:48
  • if your using a ListBox (as in my case) You just have to name the Listbox (let's say myListBox not very original isn't it), and in the code-behind you put myListBox.Datacontext = ClassWhichContainTheList; (ClassWhichContainTheList is often the viewModel of the view containning the ListBox (if you want to follow the MVVM pattern)) – astreal Jun 28 '12 at 14:55
  • Thanks again for your response. I do not believe that my datacontext is the issue. Really stuck at this point...there must be something outside of my dumbed down example that is causing me the issue. Upvoted your answer in appreciation. – mherr Jun 28 '12 at 16:27
  • Finally figured out my problem. The list that I was setting my Listbox's datacontext/itemsource to was not the class that contained my bound property. I was trying to use a separate class to store the property, and in retrospect I can see how this was a silly thing to do :) – mherr Jun 28 '12 at 19:46
  • I'm glad you finally find the solution ;). Generally when PropertyChanged is null this mean that no binding is listening to those changes (=> often a datacontext problem). – astreal Jun 29 '12 at 07:13