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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...