C# 绑定时未调用GetValue和SetValue
我编写了一个从Progressbar派生的自定义控件,该控件实现了valuechange上的动画(该值将被doubleanimation填满,直到达到目标) 使用了一个新属性“TargetValue”,因为用于ProgressBar的ControlTemplate必须在更改后直接显示新值。为此,ProgressEx包含以下内容:C# 绑定时未调用GetValue和SetValue,c#,wpf,dependency-properties,C#,Wpf,Dependency Properties,我编写了一个从Progressbar派生的自定义控件,该控件实现了valuechange上的动画(该值将被doubleanimation填满,直到达到目标) 使用了一个新属性“TargetValue”,因为用于ProgressBar的ControlTemplate必须在更改后直接显示新值。为此,ProgressEx包含以下内容: public static readonly DependencyProperty TargetValueProperty = DependencyProperty.R
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中定义的不同颜色。这真的很好,但是。现在我想在listview中使用这个条,它绑定到一些数据。问题是,在这种情况下不调用setter,所以即使通过TargetValue={Binding ProgressValue}更改了值,也不会执行动画。我知道框架总是直接调用GetValue和SetValue,不应该提供任何逻辑,但是有办法解决这个问题吗
提前感谢。依赖属性的CLR样式的getter和setter不应该被框架调用。。。它们供开发人员在代码中使用。如果您想知道
dependencProperty
值何时更改,则需要添加一个处理程序:
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
}
dependencProperty
s的CLR样式的getter和setter不应该被框架调用。。。它们供开发人员在代码中使用。如果您想知道dependencProperty
值何时更改,则需要添加一个处理程序:
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
}
From:在除异常情况外的所有情况下,包装器实现都应该分别执行GetValue和SetValue操作。原因将在主题中讨论。From:在除异常情况外的所有情况下,包装器实现应分别只执行GetValue和SetValue操作。原因将在主题中讨论。谢谢,这很有帮助。添加虚拟过程以使用新值后,控件现在可以正常工作。再次感谢。谢谢,这帮了大忙。添加虚拟过程以使用新值后,控件现在可以正常工作。再次感谢。