0

I have a textblock that is a 'status label'. I want this label to be updated, and when that happens, I want its color to be also set automatically - as well as visibility (Label invisible until it has content).

The problem is, that if I specify anything more than the text binding, then the textblock does not change (i.e. text does not appear, and it is still hidden). Actually, I tried also without binding visibility, and it appears that the Foreground also blocks the binding.

<TextBlock x:Name="StatusInfo"
                               HorizontalAlignment="Center" VerticalAlignment="Bottom"
                               FontSize="9"
                               Visibility="{Binding ElementName=ThisUc,
                                                    Path=StatusLabelVisibility}"
                               Text="{Binding ElementName=ThisUc,
                                              Path=StatusLabel}"
                               Foreground="{Binding ElementName=ThisUc,
                                                    Path=StatusLabelBrush}" />

This is all in a UserControl, so I am using dependency properties for StatusLabel property, as I want to bind it to properties in main window... Foreground and Visibility properties are not dependency properties, as I don't want to expose them. This is my property setter and getter:

public string StatusLabel
        {
            get { return (string)GetValue(StatusLabelProperty); }
            set
            {
                SetValue(StatusLabelProperty, value);
                RaisePropertyChanged("StatusLabel");
                if (value != string.Empty)
                {
                    StatusLabelVisibility = System.Windows.Visibility.Visible;
                    if (value.HasAny("success", "ok") && !value.HasAny("partial"))
                    {
                        StatusLabelBrush = Brushes.Green;
                    }
                    else if (value.HasAny("fail"))
                    {
                        StatusLabelBrush = Brushes.DarkRed;
                    }
                    else if (value.HasAny("partial"))
                    {
                        StatusLabelBrush = Brushes.DarkGoldenrod;
                    }
                    else
                    {
                        StatusLabelBrush = Brushes.Black;
                    }
                }
                else
                {
                    StatusLabelVisibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

Please let me know what am I doing wrong, perhaps that's not the way to go altogether?

Cheers

====================

While Meredith's answer solved the issue, let me just post a comment for future reference (as it was not obvious for me):

Here it goes - if you assign the UserControl property directly, not via property binding, it appears to lose the 'bound' - and if you try to change the bound property again, it won't update the control as it would have before it 'lost the bound'

Cheers

Bartosz
  • 4,406
  • 7
  • 41
  • 80
  • Have you tried changing the order of the properties you are specifying ? – Abin Jul 15 '15 at 19:36
  • @Abin - Yes, I have - as well as changing the order of statements in property setter. That didn't help - and actually, I want to understand what the principle is. – Bartosz Jul 15 '15 at 20:19

1 Answers1

3

If StatusLabel is a DependencyProperty, you can't put anything else in the setter - it won't get called correctly. Look up the way to do changed events for DependencyProperties instead. You need a PropertyChangedCallback. Check out How to use PropertyChangedCallBack. Raise your prop changes, and set all the other properties in the callback.

Community
  • 1
  • 1
Meredith
  • 173
  • 1
  • 7
  • Hi - thank you, that solved the issue - I also have a little comment on a related issue which was a bit puzzling. Here it goes - if you assign the UserControl property directly, not via property binding, it appears to lose the 'bound' - and if you try to change the bound property again, it won't update the control as it would have before it 'lost the bound' – Bartosz Jul 17 '15 at 11:07
  • Yes - if you directly assign the value, it will in fact lose the binding. You need to assign only via binding. – Meredith Jul 30 '15 at 21:06