0

How to pass property (SomeProperty) from ParentUserControl context to DependencyProperty (MyDProperty) of the ChildUserControl?

In XAML, it should be: <uc:ChildUserControl MyDProperty="{Binding SomeProperty}"> but, for some reason, MyDProperty never gets set with Parent.DataContext.SomeProperty.

In my case, I'm passing an Action, but that's not important. I think the issue lies in the binding.


ParentUserControl:

    public Action RemoveEsl1 => throw new NotImplementedException();

    <uc:ChildUserControl Title="ESL 1" RemoveEslAction="{Binding RemoveEsl1}" DataContext="{Binding Esl1}"/>

ChildUserControl:

    public static readonly DependencyProperty RemoveEslActionProperty =
        DependencyProperty.Register(nameof(RemoveEslAction), typeof(Action), typeof(ChildUserControl), new PropertyMetadata(delegate { }));


    public Action RemoveEslAction
    {
        get => (Action)GetValue(RemoveEslActionProperty);
        set => SetValue(RemoveEslActionProperty, value);
    }

I found various tips here, but none of them either suited me or worked.

9
  • How do you verify that the property "never gets set"? There is no PropertyChangedCallback where you could set a breakpoint. The property setter will not be called when the property is set by a Binding. Commented Mar 8, 2024 at 10:05
  • It is also unclear whether Esl1 is actually a property of the current DataContext, and what DataContext="{Binding Esl1}" is supposed to do when RemoveEsl1 is a property of the parent element. Please provide a minimal reproducible example. Commented Mar 8, 2024 at 10:08
  • @Clemens I call RemoveEslAction in ChildUserControl when needed, but RemoveEslAction is always null. Also, the RemoveEsl1 property is never read by anyone. Commented Mar 8, 2024 at 10:11
  • Instead of binding the ChildUserControl's DataContext, you would use a RelativeSource Binding like RemoveEslAction="{Binding RemoveEsl1, RelativeSource={RelativeSource AncestorType=ParentUserControl}}". Commented Mar 8, 2024 at 10:12
  • As a general hint, always take a look at the Output Window in Visual Studio for possible data binding error messages when you debug your application. Commented Mar 8, 2024 at 10:16

1 Answer 1

0

Answering my own question (check ParentUserControl):

Models:

public class RootModel : ViewModelBase
{
    private ParentModel parentModel = new();
    public ParentModel ParentModel { get => parentModel; set => RisePropertyChanged(ref parentModel, value); }
}

public class ParentModel : ViewModelBase
{
    private ChildModel childModel = new();
    public ChildModel ChildModel { get => childModel; set => RisePropertyChanged(ref childModel, value); }

    public string ParentModelProperty => "Correct value from ParentModel";
}

public class ChildModel : ViewModelBase
{
    private string childModelProperty = "Wrong default value from ChildModel";
    public string ChildModelProperty { get => childModelProperty; set => RisePropertyChanged(ref childModelProperty, value); }
}

MainWindow:

<Window.DataContext>
    <model:RootModel/>
</Window.DataContext>

<uc:ParentUserControl DataContext="{Binding ParentModel}"/>

ParentUserControl:

<uc:ChildUserControl ChildDependency="{Binding DataContext.ParentModelProperty, RelativeSource={RelativeSource AncestorType=UserControl}}" DataContext="{Binding ChildModel}"/>

ChildUserControl:

<StackPanel>
    <Label Content="Dependency property:"/>
    <Label Content="{Binding ChildDependency, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    <Separator/>
    <Label Content="Property:"/>
    <Label Content="{Binding ChildModelProperty}"/>
</StackPanel>


public partial class ChildUserControl : UserControl
{
    public static readonly DependencyProperty ChildDependencyProperty =
        DependencyProperty.Register(nameof(ChildDependency), typeof(string), typeof(ChildUserControl), new ("Wrong default DP value from ChildUserControl"));

    public string ChildDependency
    {
        get => (string)GetValue(ChildDependencyProperty);
        set => SetValue(ChildDependencyProperty, value);
    }

    public ChildUserControl()
    {
        InitializeComponent();
    }
}

So this is how to pass a property (ParentModelProperty) from ParentUserControl context to DependencyProperty (ChildDependency) of the ChildUserControl.

Sign up to request clarification or add additional context in comments.

1 Comment

I still don't know how to pass some value in a similar way to some property in ChildUserControl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.