質問

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

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

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?

Thanks in advance.

役に立ちましたか?

解決

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
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top