简体   繁体   English

在后面的代码中将依赖属性绑定到另一个依赖属性

[英]bind Dependency Property to Another Dependency Property in Code Behind

I have a Control Called My_A_Control and it has a property called Subject我有一个名为 My_A_Control 的控件,它有一个名为 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我有另一个名为 My_B_Control 的控件,在其模板中使用了 My_A_Control

So I want to change the value of the subject in My_A_Control through My_B_Control所以我想通过My_B_Control来改变My_A_Control中主题的值

I first created a property as follows in My_B_Control我首先在 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更新 2:此代码有效

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);

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM