Search code examples
c#wpfdata-bindinguser-controls

WPF - Data bindind - data not displayed in custom UserControl


I have a UserControl and I try to have a simple binding working

No matter what I do, the data does not display in the control as I run the app

enter image description here

public partial class UserControlA: UserControl
{
    public static readonly DependencyProperty SomeDataProperty = DependencyProperty.Register("SomeData", typeof(int), typeof(UserControlA), new PropertyMetadata(10));

    public int SomeData
    {
        get {
            return (int) GetValue(SomeDataProperty ); 
        }
        set { 
            SetValue(SomeDataProperty , value); 
        }
    }

    public UserControlA()
    {
        InitializeComponent();

        SomeData = 5;
    }
}

the xaml of the control looks like this

<UserControl x:Class="MyNameSpace.UserControlA"
             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">
        
        <StackPanel Orientation="Vertical"  >

            <Label Content="{Binding Path=SomeData , RelativeSource={RelativeSource AncestorType=UserControl}}"/>
            <TextBlock Text="{Binding Path=SomeData , RelativeSource={RelativeSource AncestorType=UserControl}}"/>
            <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=SomeData ,Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
...

any help appreciated


Solution

  • my bad, I had set a

    SomeData="{Binding SomeData, Source={StaticResource SharedViewModelInstance}}"
    

    on my component

    now data is properly displayed