在自定义用户控件的DependencyProperty上绑定不更新更新

我在自定义用户控件上进行数据绑定时遇到了困难。 我创建了一个示例项目来突出我的问题。 我是WPF的新手,也是MVVM的新手,所以请耐心等待……

我创建了一个使用数据绑定两种方式的简单视图。 内置控件上的数据绑定工作正常。 我的自定义控件没有…我在我的控件的PropertyChangedCallback中放了一个断点。 它在启动时被击中一次,但从未再次被击中。 与此同时,我已经绑定到相同价值的标签很高兴倒计时。

我错过了什么? 我的示例项目如下:

主窗口:

          

这是我的模特:

 namespace WpfMVVMApp { public class CountdownModel : INotifyPropertyChanged { private int chargeTimeRemaining_Mins; public int ChargeTimeRemaining_Mins { get { return chargeTimeRemaining_Mins; } set { chargeTimeRemaining_Mins = value; OnPropertyChanged("ChargeTimeRemaining_Mins"); } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } #endregion } } 

ViewModel:

 namespace WpfMVVMApp { public class CountdownViewModel { public CountdownModel Countdown { get; set; } DispatcherTimer timer; private const int maxMins = 360; public CountdownViewModel() { Countdown = new CountdownModel { ChargeTimeRemaining_Mins = 60 }; // Setup timers timer = new DispatcherTimer(); timer.Tick += new EventHandler(this.SystemChargeTimerService); timer.Interval = new TimeSpan(0, 0, 1); timer.Start(); } private void SystemChargeTimerService(object sender, EventArgs e) { //convert to minutes remaining // DEMO CODE - TODO: Remove this.Countdown.ChargeTimeRemaining_Mins -= 1; } } } 

这是我的用户控件的XAML:

      

这是用户控件背后的代码:

 namespace WpfMVVMApp { public partial class UserControl1 : UserControl { #region Dependency Properties public static readonly DependencyProperty MinutesRemainingProperty = DependencyProperty.Register ( "MinutesRemaining", typeof(int), typeof(UserControl1), new UIPropertyMetadata(10, new PropertyChangedCallback(minutesRemainChangedCallBack)) ); #endregion public int MinutesRemaining { get { return (int)GetValue(MinutesRemainingProperty); } set { SetValue(MinutesRemainingProperty, value); } } static void minutesRemainChangedCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args) { UserControl1 _readout = (UserControl1)property; _readout.MinutesRemaining = (int)args.NewValue; _readout.Readout.Content = _readout.MinutesRemaining; } public UserControl1() { InitializeComponent(); } } } 

您的更改回调正在破坏绑定。

作为一个骨架:在你的窗口中你有UC.X="{Binding A}"然后在那个属性改变(在UC中)你有X=B; 。 这会破坏绑定,因为在两种情况下都设置了X

要纠正,请删除更改回调并将其添加到标签:

  Content="{Binding MinutesRemaining, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 

我尝试了你的代码工作正常我唯一的改变是删除propertychangedcallback背后的代码,并将Label(读出)数据绑定到依赖属性。

用户控件(XAML)

      

USERCONTROL(代码背后)

 public partial class UserControl1 : UserControl { #region Dependency Properties public static readonly DependencyProperty MinutesRemainingProperty = DependencyProperty.Register ( "MinutesRemaining", typeof(int), typeof(UserControl1), new UIPropertyMetadata(10) ); #endregion public int MinutesRemaining { get { return (int)GetValue(MinutesRemainingProperty); } set { SetValue(MinutesRemainingProperty, value); } } public UserControl1() { InitializeComponent(); } }