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

时间:2019-07-29 08:26:02

标签: c# wpf data-binding dependency-properties

我正在编写一个名为“ 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 ...),我在用户控件的代码隐藏文件中将其定义为依赖项属性。

我的问题是,这些依赖项属性在我的“测试窗口”中正确显示,并已绑定到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));

    }
}

1 个答案:

答案 0 :(得分:0)

在您的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
        };
    }
}


MainWindow

<local:SimpleUserControl SimpleTxtProp="BBB"/>


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

InitializeComponent()

MainWindow

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

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