且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在设计中设置用户控件的自定义依赖项属性

更新时间:2022-06-03 02:26:59

Hi,



the main problem here is that you try to do things in the set accessor for Child that it actually not called when accessed from XAML. Therefore, start by cleaning up the code-behind code by removing some lines:



Hi,

the main problem here is that you try to do things in the set accessor for Child that it actually not called when accessed from XAML. Therefore, start by cleaning up the code-behind code by removing some lines:

// !!! Suspect - never call InitializeComponent from anywhere but constructor
        //public void SetChild(UIElement child)
        //{
//InitializeComponent();
    //        Child = child;


  //      }
        public UIElement Child
        {
            get { return (UIElement)GetValue(ChildProperty); }
            set
            {
                SetValue(ChildProperty, value);

                // !!! Never do anything in a dependency accessor since it will not be called when used from XAML
                // borderChildContainer.Child = value;

            }
        }





Then, by using a ContentPresenter in your Border you can use the same approach as you seem to have used for GridBackground to bind the Child:





Then, by using a ContentPresenter in your Border you can use the same approach as you seem to have used for GridBackground to bind the Child:

<Border x:Name="borderChildContainer"

                Grid.Row="1"

                Grid.Column="1" >
            <ContentPresenter Content="{Binding Child, ElementName=uc}" />
        </Border>