-2

This must be something obvious, but can anyone tell me why my the value in my label is only updated once. My PropertyChangedEventHandler never fires:

<Page.Resources>
   x:Key="SoSummaryViewModelDataSource"/>
</Page.Resources>
<Grid DataContext="{StaticResource SoSummaryViewModelDataSource}">
   <Label Grid.Row="2" 
          Margin="30, 0, 0, 0" 
          FontWeight="Medium"
          Content="{Binding TotalDisplayedCustomers, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

Here is my property:

    public string TotalDisplayedCustomers
    {
        get { return _totalDisplayedCustomers; }
        set 
        {
            if (_totalDisplayedCustomers != value)
            {
                _totalDisplayedCustomers = value;
                OnPropertyChanged("TotalDisplayedCustomers");                
            }
        }
    }

And here is my OnPropertyChanged:

    protected void OnPropertyChanged(string propertyName)
    {
        //when propertyName is TotalDisplayedCustomers, handler is null, why??
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
Jeff Anderson
  • 799
  • 7
  • 18
  • Have you tried a simple google search also look at this for some example http://stackoverflow.com/questions/17233651/wpf-data-binding-label-content – MethodMan Nov 18 '14 at 20:28
  • @user2348184 how do you change `TotalDisplayedCustomers` so _PropertyChangedEventHandler never fires_? – dkozl Nov 18 '14 at 20:31
  • @DJKRAZE yes, and i have a getter, so not sure what the use of your sample was, can you elaborate? – Jeff Anderson Nov 18 '14 at 20:35
  • @user2348184 - Most likely instances are different. Are you sure you set the value on instance `SoSummaryViewModelDataSource` only? – Rohit Vats Nov 18 '14 at 20:36
  • @user2348184 after I load my ObservableCollection, I set TotalDisplayedCustomers like this, TotalDisplayedCustomers = string.Format("Customers: {0}", _summaryLineItems.Count); – Jeff Anderson Nov 18 '14 at 20:36
  • perhaps you need to look at this very simple example and see how to bind a label http://www.codeproject.com/Tips/319869/A-Very-Simple-Example-of-Data-Binding-in-WPF – MethodMan Nov 18 '14 at 20:38
  • @RohitVats - I do clear and reload my _summaryLineItems collection, is that an issue? – Jeff Anderson Nov 18 '14 at 20:39
  • No you got me wrong. I meant you set DataContext to instance defined in xaml i.e. to SoSummaryViewModelDataSource. So, are you sure you are setting the property on same instance which you have defined in XAML as a resource? If that's not the case UI won't refresh. – Rohit Vats Nov 18 '14 at 20:44

2 Answers2

0

Well here is what I came up with, I still don't understand what was wrong with my Binding, but instead of relying on the PropertyChanged firing on a string in my view model, I instead bound my Run to the Count property of an ObservableCollection.

First I used a DataContext on my page:

<Page.DataContext>
   <vm:SoSummaryViewModel/>
</Page.DataContext>

Changed my TextBlock like this:

<TextBlock Grid.Row="2" Margin="40, 0, 0, 0">
            <Run Text="Customer Count: " FontWeight="Medium"></Run>
            <Run Text="{Binding SummaryLineItems.Count, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}">
            <TextBlock Text=" (Filtered)" Visibility="{Binding HideOnTimeCustomers, Converter={StaticResource showIfTrue}}"/>                           
</TextBlock>
Jeff Anderson
  • 799
  • 7
  • 18
0

Ha you tried to inspect yout ViewModel load into your DataContext. When i want to inspect it, i use the Wpf Inspector Tools

enter image description here

enter image description here

v20100v
  • 696
  • 4
  • 11