绑定不在Label上工作

时间:2014-11-18 20:22:09

标签: c# wpf xaml dependency-properties

这一定是显而易见的,但任何人都可以告诉我为什么我的标签中的值只更新一次。我的PropertyChangedEventHandler永远不会触发:

<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>

这是我的财产:

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

这是我的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));
        }
    }

2 个答案:

答案 0 :(得分:0)

这就是我想出来的,我仍然不明白我的Binding有什么问题,而不是依赖PropertyChanged在我的视图模型中string开火,我改为将Run绑定到Count的{​​{1}}属性。

首先,我在页面上使用了DataContext:

ObservableCollection

像这样更改了我的TextBlock:

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

答案 1 :(得分:0)

你试图检查你的ViewModel加载到你的DataContext中。当我想检查它时,我使用Wpf Inspector Tools

enter image description here

enter image description here