Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a nested user control ( user control in other user control ) . Suppose that User Control 2 is in User Control 1 ,

I'm trying to set the Dependency property of TextDependesce in the User Control 1 ,

what I have exactly: The User Control 2 contains Dependency property as shown


I'm trying to send the parameter from user control 1 into user control 2 in xaml code .
What I mean exactly set the Dependency property of TextDependesce in the other User Control 1.
how can I do that?

Note : the problem is that the TextDependecse will always take null because that the User Control 2 will initialize before the User Control 1 and this was tested using Debugging with break points , I'm searching for a solution to take the value in the user Control 1.



I will give an example :


Suppose that the User Control 1 is button and the user Control 2 is a Window , I need to set the text of button from user Control 2 , what I have as approach is send a parameter of the button text from user Control 1 to user Control 2 in order to modify it inside the UC1 that is supposed a Window .

What I have tried:

public partial class UC_Control2 : UserControl, INotifyPropertyChanged
    {
        public string TextDependecse
    {
        get { return (string)GetValue(TextDependecseProperty); }
        set { SetValue(TextDependecseProperty, value); }
    }

    public static readonly DependencyProperty TextDependecseProperty =
        DependencyProperty.Register(@"TextDependecse", typeof(string), typeof(UC_Control2), new PropertyMetadata(String.Empty));




<pre>
 <local1:UC_Control1 toog="False" TextDependecse="test1"  
            DockPanel.Dock="Top" Height="30" Width="160" FlowDirection="LeftToRight"
            Grid.Row="0" Grid.Column="2" 
            />
Posted
Updated 27-Nov-20 2:17am
v3
Comments
Richard Deeming 27-Nov-20 6:56am    
Not clear at all. Your property is defined on UC_Control2, but you are trying to set it on an instance of UC_Control1.

Click the green "Improve question" and update your question to include a clear description of the problem, along with all relevant parts of your code, and the full details of any errors.
abdou_31 27-Nov-20 8:17am    
I have updated my question
Richard Deeming 27-Nov-20 8:20am    
You are still trying to set a property on UC_Control1 when that property is only defined on UC_Control2.
abdou_31 27-Nov-20 8:29am    
Ok how can I set it on UC_Control1??

1 solution

You don't. The user controls should not know the other exists - that "forces" a shape onto the form or control they are embedded into.
Instead, create a event in one control that the parent handles. It then fetches the info from a control property and passes it to the other control.

See here: Transferring information between two forms, Part 3: Child to Child[^] - the example is WinForms, but the principle (and much of the code) is exactly the same in WPF
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900