2
votes

I have a Custom Usercontrol that has a textblock whose text will change on occasion. the TextBlocks Code is

XAML:

<TextBlock Text="{Binding ElementName=dashboardcounter, Path=Counter}" FontFamily="{Binding ElementName=dashboardcounter, Path=FontFamily}"   HorizontalAlignment="Left" Margin="17,5,0,0" VerticalAlignment="Top" FontSize="32" Foreground="#FF5C636C"/>

.cs:

private static readonly DependencyProperty CounterProperty = DependencyProperty.Register("Counter", typeof(string), typeof(DashboardCounter));

public string Counter
{
    get { return (string)GetValue(CounterProperty); }
    set { SetValue(CounterProperty, value); }
}

My Class:

private string _errorsCount;
public string ErrorsCount
{
    get { return _errorsCount; }
    set { _errorsCount = value; NotifyPropertyChanged("ErrorsCount"); }
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (null != handler)
    {
      handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

Binding of said usercontrol:

dashboardCounter.Counter = view.ErrorsCount;

The TextBlock shows - absolutly NOTHING.

what am I doing wrong? the string is dynamic and changes on occasion. It was originally an Int but I chose it to be string instead and convert my "Count" toString() instead of creating a IValueConverter

1
Could it be that ElementName value is case sensitive and that a C maj is missing in your binding : "ElementName=dashboardcounter"?AirL
x:Name="dashboardcounter" - thats not itVisualBean
It works if I set the "counter" directory dashboardCounter.Counter = "sometext";VisualBean
By using dashboardCounter.Counter = view.ErrorsCount; are you trying to bind your code behind dependency property to your class property? So that when ErrorsCount is updated, it notifies and updates Counter, that in turn notifies the view/binding?AirL

1 Answers

2
votes

By using dashboardCounter.Counter = view.ErrorsCount; you are just calling the setter of your dependency property, which in turn calls the DependencyProperty.SetValue method.

Here's the official description of it (from msdn) :

Sets the local value of a dependency property, specified by its dependency property identifier.

It sets the local value and that's all (of course following this assignment, your binding and your textblock will be updated of course).

But there's no binding creation between you Counter property and your ErrorsCount property.

So updating ErrorsCount won't update Counter and as a result your TextBlock won't be updated as well.

In your example, when dashboardCounter.Counter = view.ErrorsCount; is called probably during an initialization phase, Counter is set to string.Empty or null (assuming this a the value of ErrorsCount at that point) and will remain unchanged. No binding being created, updating ErrorsCount won't affect Counter or your view.

You have at least 3 solutions to solve your problem :

1. Directly bind your Text property to the DependencyProperty or the "INotifyPropertyChanged powered property" that is actually changing (most common case)

2. Create the needed binding programmatically by yourself instead of using dashboardCounter.Counter = view.ErrorsCount;. You'll find a short official tutorial in here and the code could look as the following one :

 Binding yourbinding = new Binding("ErrorsCount");
 myBinding.Source = view;
 BindingOperations.SetBinding(dashboardCounter.nameofyourTextBlock, TextBlock.TextProperty, yourbinding);

3. And of course, bind your ErrorsCount property to your Counter property in XAML but i don't know if it would fit your needs :

<YourDashboardCounterControl Counter="{Binding Path=ErrorsCount Source=IfYouNeedIt}"