自定义用户控件的DependencyProperty绑定不会在更改时更新 [英] Binding on DependencyProperty of custom User Control not updating on change

查看:134
本文介绍了自定义用户控件的DependencyProperty绑定不会在更改时更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在自定义用户控件上进行数据绑定。我创建了一个示例项目来突出说明我的问题。我对WPF以及MVVM都是完全陌生的,所以请忍受...

I'm having difficulties with databinding on my custom user control (s). I created an example project to highlight my problem. I'm completely new to WPF and essentially MVVM as well, so bear with me...

我创建了一个简单的视图,该视图使用两种方式进行数据绑定。内置控件上的数据绑定工作正常。我的自定义控件没有...我在控件的 PropertyChangedCallback 中设置了一个断点。它在启动时会被击中一次,但是再也不会击中。同时,我绑定到相同值的标签正在愉快地递减计数。

I created a simple view that uses databinding two ways. The databinding on the built-in control works just fine. My custom control doesn't... I put a breakpoint in the PropertyChangedCallback of my control. It gets hit once on startup, but then never again. Meanwhile, the label I have bound to the same value is happily counting down.

我缺少什么?我的示例项目如下:

What am I missing? My example project follows:

主窗口:

<Window x:Class="WpfMVVMApp.MainWindow"
        xmlns:local="clr-namespace:WpfMVVMApp"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.DataContext>
            <local:CountdownViewModel />
        </Grid.DataContext>
        <Label Name="custName" Content="{Binding Path=Countdown.ChargeTimeRemaining_Mins}" Height="45" VerticalAlignment="Top"></Label>
        <local:UserControl1 MinutesRemaining="{Binding Path=Countdown.ChargeTimeRemaining_Mins}" Height="45"></local:UserControl1>
    </Grid>
</Window>

这是我的模型:

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:

The 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控制项:

Here's the XAML for my user control:

<UserControl x:Class="WpfMVVMApp.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Label Name="Readout"></Label>
    </Grid>
</UserControl>

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

And here's the code behind the user control:

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();
        }
    }
}


推荐答案

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

Your change callback is breaking the binding.

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

As a skeleton: in your window you have UC.X="{Binding A}" and then in that property change (in UC) you have X=B;. This breaks the binding since in both cases you set X.

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

To rectify, remove change callback and add this to the label:

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

这篇关于自定义用户控件的DependencyProperty绑定不会在更改时更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆