0

So i got this Control:

CharacterMapControl.xaml:

<UserControl x:Class="CharacterMap.CharacterMapControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:CharacterMap">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="350"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <TextBlock Text=""></TextBlock>
        </StackPanel>
    </Grid>


</UserControl>

Then the CharacterMapControl.xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace CharacterMap
{
    /// <summary>
    /// Interaction logic for CharacterMapControl.xaml
    /// </summary>
    ///     
    public partial class CharacterMapControl : UserControl 
    {
        public static readonly DependencyProperty FilepathProperty = DependencyProperty.Register("Filepath", typeof(string), typeof(CharacterMapControl));
        public string Filepath
        {
            get { return (string)GetValue(FilepathProperty); }
            set { SetValue(FilepathProperty, value); }
        }



        public CharacterMapControl()
        {
            InitializeComponent();
        }
    }
}

This is inside a WPF User Control Library for .NET Core.

Then i added a new WPF App .NET Core Project and edited the MainWindow.xaml to look like this:

<Window x:Class="WPF_Control_Tester.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:charactermap="clr-namespace:CharacterMap;assembly=CharacterMap"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <charactermap:CharacterMapControl Filepath="D:\\repos\\WpfProjects\\latinchars.xml"></charactermap:CharacterMapControl>
    </Grid>
</Window>

Well - now the Filepath inside the CharacterMapControl.xaml.cs is always null. I don't understand why. It's bound properly and should map to the Filepath i added in the MainWindow or?

Flo
  • 325
  • 2
  • 6
  • 18

2 Answers2

1

When the CharacterMapControl is constructed, the dependency property value will be null, as the default value is not specified while defining the dependency property.

Little after constructing the control CharacterMapControl, the loaded event will be raised, at this point the dependency properties will have initialized values.

Modifing the constructor as below will help in understanding more.

        public CharacterMapControl()
        {
            InitializeComponent();

            var y = GetValue(FilepathProperty);
            Console.WriteLine(y);

            this.Loaded += (sender, args) =>
            {
                var x = GetValue(FilepathProperty);
                Console.WriteLine(x);
            };
        }
1

You haven't bound the Text property of your TextBlock to anything.

When I tried your code I added the binding:

        <TextBlock Text="{Binding Filepath, RelativeSource={RelativeSource AncestorType=UserControl}}"/>

Which works

Andy
  • 11,864
  • 2
  • 17
  • 20
  • Well i don't understand why then all of a sudden it works... but it does. Thanks for that! Altough the solution of @nachiappan-kumarappan works too and makes more sense :D How can i use this without actually binding the dep prop? – Flo Oct 08 '19 at 11:52
  • Why would you choose not to bind it? One of the huge plusses of dependency properties is binding. If you're unfamiliar with it I recommend you adopt the mvvm pattern. – Andy Oct 08 '19 at 12:00
  • I actually just want to be able to use it as a parameter from the MainWindow.xaml – Flo Oct 08 '19 at 12:31
  • You can just make it a regular public property on your usercontrol and work with it in the Loaded event, public string FilePath { get; set; } – Andy Oct 08 '19 at 13:11
  • Well i guess that i was thinking way too complicated there^^ Thanks for the help Andy! – Flo Oct 08 '19 at 13:25