Microsoft WPF 设计器的属性值在被另一个属性更改时不会更新

如何解决Microsoft WPF 设计器的属性值在被另一个属性更改时不会更新

针对 WinForms 项目提出了类似的问题,但针对 WPF 项目提出了类似问题。

下面这个非常简单的示例显示了这个问题。我拥有的实际用户控件要复杂得多,但此示例显示了我遇到的基本问题。

UserControl1 xaml(没有任何东西,因为它不需要任何东西来显示问题)

<UserControl x:Class="TestProperty.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
  <Grid>
  </Grid>
</UserControl>

Window xaml(只包含一个 UserControl1):

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:TestProperty="clr-namespace:TestProperty;assembly=TestProperty" x:Class="TestApp.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
  <Grid>
    <TestProperty:UserControl1 HorizontalAlignment="Left" Height="100" Margin="10,10,0" VerticalAlignment="Top" Width="100"/>
  </Grid>
</Window>

实际的 Window 代码隐藏实际上什么都没有

using System.Windows;

namespace TestApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

这是 UserControl1 的代码隐藏。有两个属性,其中第一个属性只是将第二个属性修改为相同。

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace TestProperty
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        [Description("Sets a test property value that sets another property.")]
        [Category("UserControl1")]
        [RefreshProperties(RefreshProperties.All)]
        public bool TestValue1
        {
            get => (bool) GetValue(TestValue1Property);
            set => SetValue(TestValue1Property,value);
        }
        public static readonly DependencyProperty TestValue1Property =
            DependencyProperty.Register(
                "TestValue1",typeof(bool),typeof(UserControl1),new FrameworkPropertyMetadata(false,new PropertyChangedCallback(TestValue1Changed)));
        private static void TestValue1Changed(DependencyObject d,DependencyPropertyChangedEventArgs e) =>
            ((UserControl1) d)?.TestValue1Changed(e);
        private void TestValue1Changed(DependencyPropertyChangedEventArgs e)
        {
            TestValue2 = (bool) e.NewValue;
            InvalidateProperty(TestValue2Property);
        }

        [Description("Value should change automatically when TestValue1 changes.")]
        [Category("UserControl1")]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool TestValue2
        {
            get => (bool) GetValue(TestValue2Property);
            set => SetValue(TestValue2Property,value);
        }
        public static readonly DependencyProperty TestValue2Property =
            DependencyProperty.Register(
                "TestValue2",new PropertyChangedCallback(TestValue2Changed)));
        private static void TestValue2Changed(DependencyObject d,DependencyPropertyChangedEventArgs e) =>
            ((UserControl1) d)?.TestValue2Changed(e);
        private void TestValue2Changed(DependencyPropertyChangedEventArgs e)
        {
            InvalidateProperty(TestValue2Property);
        }
    }
}

属性 RefreshProperties 和对 InvalidateProperty 的调用似乎没有任何作用。

编辑 MainWindow.xaml 并选择 UserControl1 时,属性窗口的 UserControl1 部分显示 TestValue1TestValue2。如果 TestValue1 改变了,TestValue2 不会改变。但是,如果您切换到另一个 Visual Studio(或 Blend)编辑器窗口,然后返回到 MainWindow.xaml,则 TestValue2 将与 TestValue1.

TestValue1 强制更新时,我似乎找不到任何方法来自动更新 TestValue2。实际情况显然更复杂,有很多控件,当添加我们的 UserControl 时,我们希望设计者能够指定几个可能导致强制(或强制)其他属性的属性。但是,设计人员需要能够看到属性值的变化,而不必切换到另一个文件并再次返回以重新绘制设计人员的属性窗口。

这是根本无法完成的事情 - 还是我遗漏了什么。请注意,这是一个 UserControl,并不是为了操作 MVVM 本身而设计的,而是允许使用带有(或不带有)MVVM 设计的控件来设计另一个窗口。

此外,在“某种”相关问题中。我们有一个属性需要在与 WidthHeight 相同的属性中有一个条目,并带有 auto 按钮。当设置为 Auto 时,显示 “Auto (nnn)” 作为自动设置值的变化 nnn 变化。未设置时,该值只是 nnn 并在使用 UserControl 设计窗口期间定义。这可能吗?

在此先感谢任何可以阐明这一点的人。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res