繁体   English   中英

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等)在我的用户控件的Code-Behind-File中定义为依赖属性。

我的问题是, 只有那些Dependency-Properties才能在Test-Window中正确显示,这些依赖项绑定在UserControl-XAML中,因此由initializeComponent()调用,例如TitleOfChart 正确显示是指: 显示XAML的设计时值,其中包括UserControl 其他依赖项属性无法正确显示,因为它们仅显示其默认值。

从我的示例中,您将看到对虚拟计算的评估将使用“依赖项属性”的默认值进行。 不,在获得“设计时值”之后,从Main.xaml中读出。

谢谢您的帮助!

编辑:可复制代码(4个文件)

文件1和2:使用控件的窗口及其代码隐藏

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"/>


无论您考虑依赖项还是简单属性,都没有关系。

MainWindow InitializeComponent()

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

因此,一旦加载了“ SimpleUserControl”的等式,它就会具有在MainWindow XAML中设置的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM