WPF:如何使用父 XAML 的设计时间值重新加载自定义 属性 的默认值?

WPF: How can I reload the Custom Property's Default Value with it's Design-Time-Value from the parent XAML?

我正在编写一个名为 "GridAndChart" 的用户控件,它可以像这样在设计时填充值:

    <local:GridAndChart HorizontalAlignment="Left" 
                        VerticalAlignment="Top" Width="660"
                        Height="342" 
                        Margin="28,29,0,0"
                        NameX="Auslastung in %"
                        NameY="Wirkungsgrad in %"
                        MinValueX="0"
                        MaxValueX="100"
                        MinValueY="0"
                        MaxValueY="100"
                        TitleOfChart="Kennlinie Wirkungsgrad"
                        Xvals='12,323,43,55,65,7'
                        Yvals='60,99,99,99,99,99'
                        />

您可以在那里看到的大多数属性(如 TitleOfChart、Xvals 等)我在我的用户控件的代码隐藏文件中定义为依赖属性。

我的问题是只有那些依赖属性在我的测试-Window中正确显示,它们绑定在 UserControl-XAML 和因此由 initializeComponent() 调用,例如 TitleOfChart。我所说的正确显示是指:显示来自 XAML 的设计时值,其中包含我的 UserControl。其他依赖项属性未正确显示,因为它们仅显示其默认值。

从我的示例中,您将看到虚拟计算的评估将使用依赖属性的默认值进行。不,之后他们得到了他们的 "Design-Time-Values",从 Main.xaml.

中读取出来

感谢您的帮助!

编辑:可重现代码(4 个文件)

文件 1 和 2:Window 使用控件及其代码隐藏

MainWindow.xaml

<Window x:Class="MinimalReproducibleExample.MainWindow"
    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:local="clr-namespace:MinimalReproducibleExample"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:SimpleUserControl Working="I defined this Text at Design time" NotWorking="1,2,3,4"/>
    </Grid>
</Window>

MainWindow.xaml.cs(我留空了)

using ...
namespace MinimalReproducibleExample
{
public partial class MainWindow : Window
    {
    public MainWindow()
    {
            InitializeComponent();
        }
    }
}

文件 3 和 4:用户控件及其代码隐藏

SimpleUserControl.xaml

    <UserControl x:Class="MinimalReproducibleExample.SimpleUserControl"
         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" 
         xmlns:local="clr-namespace:MinimalReproducibleExample"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>
        <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <TextBox Grid.Row="0" Name="TextBox1XAML" />
    <TextBox Grid.Row="1" Name="TextBox2XAML" />
</Grid>
</UserControl>

SimpleUserControl.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MinimalReproducibleExample
{
public partial class SimpleUserControl : UserControl
{
    public SimpleUserControl()
    {
        InitializeComponent();

        string notWorking = NotWorking;//just to show that there happens some calculation with the values
        int a = Convert.ToInt32(notWorking.ElementAt(0)); //before they are displayed
        int b = Convert.ToInt32(notWorking.ElementAt(1));
        int c = a + b;
        TextBox2XAML.Text = c.ToString();
    }
    public string Working
    {
        get { return GetValue(WorkingProperty).ToString(); }
        set { SetValue(WorkingProperty, value); }
    }
    public static readonly DependencyProperty WorkingProperty = DependencyProperty.Register(nameof(Working), typeof(string), typeof(SimpleUserControl), new PropertyMetadata("The default is working.", (s, e) => (s as SimpleUserControl).TextBox1XAML.Text = (string)e.NewValue));

    public string NotWorking
    {
        get { return GetValue(NotWorkingProperty).ToString(); }
        set { SetValue(NotWorkingProperty, value); }
    }

    public static readonly DependencyProperty NotWorkingProperty = DependencyProperty.Register(nameof(NotWorking), typeof(string), typeof(SimpleUserControl), new PropertyMetadata("The default is also working.", (s, e) => (s as SimpleUserControl).NotWorking = (string)e.NewValue));

    }
}

在您的 MCVE 中,如果您将代码放入用户控件的 Loaded 事件处理程序,那么一切都会按预期工作。参见示例:


用户控件

public partial class SimpleUserControl : UserControl
{
    public string SimpleTxtProp { get; set; } = "AAA";

    public SimpleUserControl()
    {
        //Constructor being called from InitializeComponent() of parent element. All properties are initialized with values from code behind,
        //after object creation is finished, it's property will be initialized with values set in parent element for this control.
        InitializeComponent();//Content of control being initialized: constructor -> InitializeComponent()

        var isAAA = SimpleTxtProp == "AAA";//true

        Loaded += (o, e) =>
        {
            var isBBB = SimpleTxtProp == "BBB"; //true
        };
    }
}


主窗口

<local:SimpleUserControl SimpleTxtProp="BBB"/>


考虑依赖还是简单属性都无所谓。

InitializeComponent() of MainWindow 确实

  • 触发SimpleUserControl的实例化,因此构造函数 默认情况下 values/values 来自后面被调用的代码,
  • 然后设置创建对象的属性。

因此,一旦 "SimpleUserControl" 的实例被加载,它就会具有在 MainWindow 的 XAML 中设置的值。