的GetValue和的SetValue不叫绑定 [英] GetValue and SetValue is not called on binding

查看:138
本文介绍了的GetValue和的SetValue不叫绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的进度派生的自定义控制,实现了对valuechange动画(该值,直到达到目标以DoubleAnimation是填补了)。

I wrote a custom control derived from Progressbar which implements animations on valuechange (the value fills up with a doubleanimation until the target is reached).

var duration = new Duration(TimeSpan.FromSeconds(2.0));
var doubleanimation = new DoubleAnimation(value, duration)
{
     EasingFunction = new BounceEase()
};
BeginAnimation(ValueProperty, doubleanimation);

一个新的属性TargetValue时,因为的Con​​trolTemplate用于进度已显示新改变后直价值。对于这个ProgressEx包含以下内容:

A new Property "TargetValue" is used, because the ControlTemplate used for the ProgressBar has to show the new value straight after changing it. For this the ProgressEx contains the following:

public static readonly DependencyProperty TargetValueProperty = DependencyProperty.Register("TargetValue", typeof (int), typeof (ProgressEx), new FrameworkPropertyMetadata(0));
    public int TargetValue
    {
        get { return (int)GetValue(TargetValueProperty); }
        set 
        {
            if (value > Maximum)
            {
                //Tinting background-color
                _oldBackground = Background;
                Background = FindResource("DarkBackgroundHpOver100") as LinearGradientBrush;
            } 
            else
            {
                if (_oldBackground != null)
                    Background = _oldBackground;
            }

            SetValue(TargetValueProperty, value);
            Value = value;
        }
    }

在该TargetValue超过最大我会用不同的在XAML中定义的颜色。这工作真不错 - 但是。现在我想在它被绑定到一些数据的列表视图用这个吧。问题是,该设定器未在此情况下调用,所以不执行动画,即使当值通过TargetValue = {结合ProgressValue}改变。我知道,该框架将始终调用的GetValue和的SetValue直接,没​​有逻辑应该提供的,但有没有办法解决?

When the TargetValue exceeds the maximum i will use a different color defined in xaml. This works really good - But. Now i want to use this bar in a listview where it is bind to some data. Problem is, the setter is not called in this case, so no animation is executed, even when the value is changed via TargetValue={Binding ProgressValue}. I know that the framework will always call GetValue and SetValue directly and no logic should be supplied, but is there a way around this?

先谢谢了。

推荐答案

并不意味着的DependencyProperty S的CLR风格的getter和setter被称为框架...他们在那里为开发者在代码中使用。如果你想知道当一个的DependencyProperty 值发生了变化,你需要添加一个处理程序:

The CLR style getters and setters of DependencyPropertys are not meant to be called by the Framework... they are there for developers to use in code. If you want to know when a DependencyProperty value has changed, you need to add a handler:

public static readonly DependencyProperty TargetValueProperty = 
DependencyProperty.Register("TargetValue", typeof (int), typeof (ProgressEx), 
new FrameworkPropertyMetadata(0, OnTargetValueChanged));

private static void OnTargetValueChanged(DependencyObject dependencyObject, 
DependencyPropertyChangedEventArgs e)
{
    // Do something with the e.NewValue and/or e.OldValue values here
}

这篇关于的GetValue和的SetValue不叫绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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