1

I have a Control Called My_A_Control and it has a property called Subject

public class My_A_Control : Control

public static readonly DependencyProperty SubjectProperty = DependencyProperty.Register(
         "Subject", typeof(string), typeof(My_A_Control), new PropertyMetadata(Confirm);

and i have another control called My_B_Control that Inside its template, My_A_Control is used

So I want to change the value of the subject in My_A_Control through My_B_Control

I first created a property as follows in My_B_Control

public class My_B_Control : Control

public static readonly DependencyProperty SubjectProperty =
           My_A_Control.SubjectProperty.AddOwner(typeof(My_B_Control));

        public string Subject
        {
            get { return (string) GetValue(SubjectProperty); }
            set { SetValue(SubjectProperty, value); }
        }

And then I connected them as follows

public My_B_Control()
{
  var ctl = new My_A_Control
   {
     Subject = Subject
   };
}

But this method does not work

update:

 <Style TargetType="local:My_B_Control">
                <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:My_B_Control">
                    <Border x:Name="templateRoot" Background="{TemplateBinding Background}">
                        <Grid x:Name="PART_Root" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="30"/>
                            </Grid.ColumnDefinitions>
                            <Popup Grid.Column="0" VerticalOffset="4" x:Name="PART_Popup" StaysOpen="False"/>
                        </Grid>
...

and

public My_B_Control()
    {
      var ctl = new My_A_Control
       {
         Subject = Subject
       };
 _popup.Child = ctl;
    }

update 2: this code work

Binding myBinding = new Binding();
            myBinding.Source = this;
            myBinding.Path = new PropertyPath("Subject");
            myBinding.Mode = BindingMode.TwoWay;
            myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            BindingOperations.SetBinding(ctl, My_A_Control.SubjectProperty, myBinding);
6
  • 1
    "Inside its template" would typically mean to use a TemplateBinding. How does the ControlTemplate look like? Commented May 12, 2021 at 15:29
  • 1
    @Clemens There is a <Popup in My_B_Control template and I create My_A_Control inside popup with C# code Commented May 12, 2021 at 15:32
  • 1
    Show us the XAML and code please. Or search StackOverflow for "binding in code". Commented May 12, 2021 at 15:32
  • 1
    @Clemens please see my question again Commented May 12, 2021 at 15:37
  • 1
    And the search results won't help? Commented May 12, 2021 at 15:38

1 Answer 1

3

Something like this should work:

public My_B_Control()
{
     var ctl = new My_A_Control();

     ctl.SetBinding(My_A_Control.SubjectProperty, new Binding
     {
         Path = new PropertyPath("Subject"),
         Source = this
     });

     _popup.Child = ctl;
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank you, please see my question, i found a code that work fine but i want to know is it ok or not?
That's the same. With more Binding properties that you probably don't need. Is the Binding supposed to be TwoWay? If not, don't set it. UdateSourceTrigger is totally pointless here.
I want the user to be able to change the value in xaml with hotreload, Do i need a two-way binding?
I can't tell. It doesn't hurt to set it.

Your Answer

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