WPF 和自动重绘
我有一个自定义 WPF Line 及其样式。 UserControl 资源:
<!-- Framework properties to update -->
<SolidColorBrush x:Key="MyLineBrush" Color="LightGreen" />
<sys:Double x:Key="MyLineStrokeThickness">1</sys:Double>
<!-- Custom property to update -->
<sys:Boolean x:Key="MyLineIsArrowUsed">false</sys:Boolean>
<Style TargetType="local:MyLine" x:Key="MyLineStyleKey">
<!-- AutoUpdates the control -->
<Setter Property="Fill"
Value="{DynamicResource MyLineBrush}"/>
<Setter Property="StrokeThickness"
Value="{DynamicResource MyLineStrokeThickness}" />
<!-- does NOT AutoUpdate the control -->
<Setter Property="ShowText"
Value="{DynamicResource MyLineIsArrowUsed}"/>
现在,我观察到,当我更新 MyLineStrokeThickness 时,控件会立即更新。 但是当我更新自定义依赖属性 MyLineIsArrowUsed
时,没有任何变化。
一旦更新了自定义依赖属性,我应该使用什么来更新自定义控件(线条)绘图?
我尝试过:
static void OnIsArrowUsedChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
var s = (MyLine)source;
s.UpdateLayout();
}
但这不起作用。
仅当我移动控件或在强制重绘自身时执行其他操作时,控件才会更新。
I have a custom WPF Line and its style. UserControl Resources:
<!-- Framework properties to update -->
<SolidColorBrush x:Key="MyLineBrush" Color="LightGreen" />
<sys:Double x:Key="MyLineStrokeThickness">1</sys:Double>
<!-- Custom property to update -->
<sys:Boolean x:Key="MyLineIsArrowUsed">false</sys:Boolean>
<Style TargetType="local:MyLine" x:Key="MyLineStyleKey">
<!-- AutoUpdates the control -->
<Setter Property="Fill"
Value="{DynamicResource MyLineBrush}"/>
<Setter Property="StrokeThickness"
Value="{DynamicResource MyLineStrokeThickness}" />
<!-- does NOT AutoUpdate the control -->
<Setter Property="ShowText"
Value="{DynamicResource MyLineIsArrowUsed}"/>
Now, I observed when I update the MyLineStrokeThickness
the control is updated instantly.
but when I update my custom dependency property MyLineIsArrowUsed
there are no changes.
What should I use in order to update the custom control (line) drawing once my custom dependency property is updated?
I tried:
static void OnIsArrowUsedChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
var s = (MyLine)source;
s.UpdateLayout();
}
but this does not work.
The control updates only when I move it or do other actions when it is forced to redraw itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在依赖项属性声明中添加 FrameworkPropertyMetadata.AffectsMeasure
http:// msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadata.affectsmeasure.aspx
In the dependency property declaration add FrameworkPropertyMetadata.AffectsMeasure
http://msdn.microsoft.com/en-us/library/system.windows.frameworkpropertymetadata.affectsmeasure.aspx