2015-09-03 32 views
1

我一直在使用MVVM我的WPF应用程序,现在一段时间的价值,但我试图保持禁用,直到一个按钮,就可以启用。没问题,我只是在ViewModel中做一个布尔,并将其绑定到该模型中。RaisePropertyChanged()不更新单个按钮

布尔:

private bool m_EnableLoadQuickStatsButton; 
    public bool EnableLoadQuickStatsButton 
    { 
     get { return m_EnableLoadQuickStatsButton; } 
     set { m_EnableLoadQuickStatsButton = value; RaisePropertyChanged("EnableLoadQuickStatsButton"); } 
    } 

XAML按钮:

   <Button Margin="5" 
         FontSize="14" 
         IsEnabled="{Binding EnableLoadQuickStatsButton}"> 
        Load Quickstats 
       </Button> 

的DataContext宣言:

DataContext="{StaticResource MainScreenViewModel} 

.gif注意设计师和例如按钮的被使能/被初始属性值禁用:http://i.imgur.com/lpVeEBd.gif

该按钮是启用/禁用基于任何属性开始作为。但是,更改属性不会启用/禁用我的按钮。为什么是这样?我似乎没有任何其他控件和属性的问题。

编辑:

我的正常类实现INotifyPropertyChanged,有有在按预期工作我的WPF应用程序绑定到控制它们的值等性能。

EDIT2:应用程序的A.GIF,表示在数据绑定工作它的其他部分:http://i.imgur.com/lIvzHv7.gif

EDIT3:

System.Windows.Data Warning: 56 : Created BindingExpression (hash=51180192) for Binding (hash=56315736) 
System.Windows.Data Warning: 58 : Path: 'EnableLoadQuickStatsButtonTest' 
System.Windows.Data Warning: 60 : BindingExpression (hash=51180192): Default mode resolved to OneWay 
System.Windows.Data Warning: 61 : BindingExpression (hash=51180192): Default update trigger resolved to PropertyChanged 
System.Windows.Data Warning: 62 : BindingExpression (hash=51180192): Attach to System.Windows.Controls.Button.IsEnabled (hash=23804398) 
System.Windows.Data Warning: 67 : BindingExpression (hash=51180192): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=51180192): Found data context element: Button (hash=23804398) (OK) 
System.Windows.Data Warning: 78 : BindingExpression (hash=51180192): Activate with root item MainScreenViewModel (hash=64564967) 
System.Windows.Data Warning: 108 : BindingExpression (hash=51180192): At level 0 - for MainScreenViewModel.EnableLoadQuickStatsButtonTest found accessor RuntimePropertyInfo(EnableLoadQuickStatsButtonTest) 
System.Windows.Data Warning: 104 : BindingExpression (hash=51180192): Replace item at level 0 with MainScreenViewModel (hash=64564967), using accessor RuntimePropertyInfo(EnableLoadQuickStatsButtonTest) 
System.Windows.Data Warning: 101 : BindingExpression (hash=51180192): GetValue at level 0 from MainScreenViewModel (hash=64564967) using RuntimePropertyInfo(EnableLoadQuickStatsButtonTest): 'False' 
System.Windows.Data Warning: 80 : BindingExpression (hash=51180192): TransferValue - got raw value 'False' 
System.Windows.Data Warning: 89 : BindingExpression (hash=51180192): TransferValue - using final value 'False' 

Edit4:在启动调试输出创建一个新项目,它按预期工作,只是因为某些原因而不在我目前的项目上。

+1

什么是你的'RaisePropertyChanged'吗?你的班级是否正确实施了'INotifyPropertyChanged'? – nvoigt

+0

是的,所有其他属性和控件都能正常工作。除此之外,没有理由为什么。 –

+0

是否正确设置了DataContext的视图属性? –

回答

2

正如在注释中指出的,似乎ViewModel正在被实例化几次,并且按钮的DataContext中的那个与正在更改的实例不同。

简单的试验证明了这一点。

至于一些扩展的提示...后面使用MVVM的想法是分离从模型视图。我会避免拥有名称暗示“按钮”或任何其他视觉元素的属性。

EnableLoadQuickStatsButton在视图模型应该被称为像AreQuickStatsReadyToBeLoaded或类似的东西

+1

此外,OP会更好地将按钮绑定到返回ICommand的属性。 – slugster

+0

@slugster确实,并使用'CanExecute'而不是设置'IsEnabled' – Jcl

+0

我一定会尝试做到这一点! –

0

我尝试了相同的代码,它只是运行良好。

的代码应该是

EnableLoadQuickStatsButton = AFunctionReturnsBool();

更改属性的时候,也许你使用

m_EnableLoadQuickStatsButton = AFunctionReturnsBool();

+0

谢谢你的回复。双重检查,我也刚刚创建了一个新项目,它工作正常。我肯定会更改'EnableLoadQuickStatsButton'。为了保险起见,在更改bools值后,我继续为viewmodel中的每个属性调用'RaisePropertyChanged'。我也做了适当的断点,并可以验证布尔已被更改。 –

+0

并确保按钮的容器已启用? – Zhu

+0

容器,你的意思是它的面板?它目前位于启用的DockPanel中,是的。 –