0

I am writing a User Control named "GridAndChart" that can be filled with values at Design-Time just like this:

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

Most of the Properties you can see there (Like TitleOfChart, Xvals,...) I defined in the Code-Behind-File of my User-Control as Dependency-Properties.

My Problem is that only those Dependency-Properties are displayed correctly in my Test-Window, that are bound in the UserControl-XAML and therefore invoked by the initializeComponent(), like for example TitleOfChart. By displayed correctly I mean: Display of the Design-time-value from the XAML where I include my UserControl. The other dependency properties don't display correctly because they show only their default value.

From my example you will see that the Evaluation of my dummy-calculations will happen with the default values of the Dependency Properties. Not, after that they got their "Design-Time-Values", read out from the Main.xaml.

Thankyou for your help!

EDIT: Reproducible Code (4 Files)

File 1 and 2: The Window that uses the Control and it's Code-Behind

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 (I left it empty)

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

File 3 and 4: The User-Control and it's Code-Behind

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));

    }
}
Verena
  • 19
  • 6
  • 1
    I can't follow you. "But for example the Xvals, that are **not directly displayed** but converted to ChartValues, are well converted (as I saw in the debugger) but the **values get never displayed**."? If the values are intentionally not displayed, then why they should be displayed? Can you post an [MCVE](https://stackoverflow.com/help/mcve) to reproduce the issue? – Rekshino Jul 29 '19 at 13:51
  • Thankyou for your comment and your answer, I will post the reproducible code in my question above. – Verena Jul 29 '19 at 15:43
  • See my edited answer.. – Rekshino Jul 30 '19 at 06:59
  • See again: "I would like to know how the **values of the dependency-properties are not filled with their default-values** but with their real ones before the Initialization" "the Evaluation of my dummy-calculations **will happen with the default values of the Dependency Properties**" I would offer to delete the first part of your post and leave only part after **Edit**. – Rekshino Jul 30 '19 at 08:02

1 Answers1

0

In your MCVE if you put the code to the Loaded event handler of your usercontrol, then all will work as expected. See example:


Usercontrol:

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


It doesn't matter whether you consider dependency or simple property.

InitializeComponent() of MainWindow does

  • trigger the instantiation of SimpleUserControl, hence constructor with default values/values from code behind being called,
  • then the properties of the created object being set.

So once an istance of "SimpleUserControl" is loaded, it has the values, which was set in XAML of MainWindow.

Rekshino
  • 6,954
  • 2
  • 19
  • 44