Silverlight /WPF:DependencyProperty.AddOwner &更改时渲染
我需要实现一个标尺,并且有一个从 Control 派生的 CustomControl RangeSlider,它具有多个 DependencyProperties(例如,Minimum、Maximum、LowerValue、UpperValue)。
public static readonly DependencyProperty LowerValueProperty =
DependencyProperty.Register("LowerValue",
typeof(double),
typeof(RangeSlider),
new FrameworkPropertyMetadata(0d, OnLowerValueChanged, CoerceLowerValue));
public double LowerValue
{
get { return (double)base.GetValue(LowerValueProperty); }
set { base.SetValue(LowerValueProperty, value); }
}
在Xaml中,Ruler有一个子控件Scale(派生自Canvas),它需要相同的Properties来绘制Scaling。如果 LowerValue 或 UpperValue 发生更改,则应重新绘制比例。所以我添加
public static readonly DependencyProperty LowerValueProperty =
RangeSlider.LowerValueProperty.AddOwner(typeof(Scale), new FrameworkPropertyMetadata(1d));
当我移动拇指时,比例中应该出现一些东西。首先,我实现了 OnRender(DrawingContext) 并将其添加到 RangeSlider FrameworkPropertyMetadataOptions 中的 LowerValueProperty 中。但没关系,什么也没有发生。此外,我不想使用 OnRender(DrawingContext),因为它在 Silverlight 中不可用。
此外,我想知道为什么 RangeSlider 中的 OnLowerValueChanged-Method 会被引发,但 LowerValue 的设置器不会被引发(至少调试器不会在那里中断)。
那么,有没有办法让Silverlight &当 RangeSlider 中的 LowerValue 更改时(除了从 OnLowerValueChanged 引发之外),WPF 在 Scale 中自动引发方法?
注意:上面的代码是WPF代码。在 Silverlight 中当然有一些区别,因为 Silverlight 不支持强制转换。
因为代码应该在 Silverlight 和 WPF 中运行,所以我不想使用 OnRender(DrawingContext)。
I need to implement a Ruler and I have a CustomControl RangeSlider which is derived from Control and has several DependencyProperties (e.g. Minimum, Maximum, LowerValue, UpperValue).
public static readonly DependencyProperty LowerValueProperty =
DependencyProperty.Register("LowerValue",
typeof(double),
typeof(RangeSlider),
new FrameworkPropertyMetadata(0d, OnLowerValueChanged, CoerceLowerValue));
public double LowerValue
{
get { return (double)base.GetValue(LowerValueProperty); }
set { base.SetValue(LowerValueProperty, value); }
}
In Xaml, the Ruler has a sub control Scale (derived from Canvas), which needs the same Properties to draw the Scaling. If LowerValue or UpperValue changed, the Scale should repaint. So I add
public static readonly DependencyProperty LowerValueProperty =
RangeSlider.LowerValueProperty.AddOwner(typeof(Scale), new FrameworkPropertyMetadata(1d));
When I move the thumb, something should raised in Scale. First I implemented OnRender(DrawingContext) and added to the LowerValueProperty in RangeSlider FrameworkPropertyMetadataOptions. But it doesn't matter, nothing happens. Moreover I do not want to use OnRender(DrawingContext), because it's not available in Silverlight.
Furthermore I wonder why the OnLowerValueChanged-Method in RangeSlider is raised but not the setter of LowerValue (at least the debugger does not break in there).
So, is there a way for Silverlight & WPF to raise a method automatically in Scale, when LowerValue in RangeSlider changed (except raising from OnLowerValueChanged)?
Note: The above code is the WPF-Code. In Silverlight there are of course some difference, because Silverlight does not support among other things Coercion.
Because the code should run in Silverlight and WPF, I do not want to use OnRender(DrawingContext).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您希望能够在属性中执行一些逻辑,这些逻辑只是 GetValue 和 GetValue 的包装器。设置值方法。问题是它们仅在您创建 XAML 并指定这些属性的绑定时使用。在幕后,它们不再被调用,而是 GetValue 和 GetValue 。使用SetValue。
这就是为什么我认为(仍然不确定我是否清楚地理解问题)您必须在控件内使用
ValueChanged
事件(例如)的事件处理程序,并且没有其他方法可以拦截对 GetValue 和 GetValue 的调用。设置值。或者您可以将
PropertyChangedCallback
与 DP 更改后调用的 DependencyProperties 一起使用,并将代码放在那里If I understand correctly you want to be able to perform some logic in propertiers, that are just wrappers around GetValue & SetValue methods. Problem is they are only used when you create XAML and specify bindings for those properties. Behind the scenes they are never called anymore, instead GetValue & SetValue are used.
Thats why I think (still not sure I understand question clearly) you have to either use event handlers for
ValueChanged
event (for example) inside your control and there is no other way to intercept calls to GetValue & SetValue.Or you can use
PropertyChangedCallback
with DependencyProperties that is called after DPs are changed and place your code there