WPF 和自动重绘

发布于 2024-10-02 09:39:17 字数 1304 浏览 0 评论 0原文

我有一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗酒趁年少 2024-10-09 09:39:17

在依赖项属性声明中添加 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文