我有一个想法,尝试了一下,对我有用。我只是想与其他人分享这个想法。希望它有用。
视频链接解释了解决方案的最终结果: 视频链接![输入图片描述](https://istack.dev59.com/LvMdK.gif)
基本思路是创建UIElement DependencyProperty而不是创建Border DependencyProperty
首先,您应该向用户控件(在您的情况下为"MyUserControl")添加边框、面板或任何您想要的内容,并确保它有一个名称以从.cs文件访问:
<Border x:Name="LeftBorder" Grid.Column="0">
然后,您应该在用户控件(在您的情况下是"MyUserControl")中添加一个公共UIElement值:
public UIElement LeftBorderChild
{
get { return (UIElement)GetValue(LeftBorderChildProperty ); }
set { SetValue(LeftBorderChildProperty , value); }
}
其次,您的
DependencyProperty类型必须是UIElement:
public static readonly DependencyProperty LeftBorderChildProperty = DependencyProperty.Register("LeftBorderChild", typeof(UIElement), typeof(MyUserControl), new PropertyMetadata(new PropertyChangedCallback(LeftBorderChildChanged)));
在这之后,是键盘输入事件:
public static void LeftBorderChildChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyUserControl thisUserControl = d as MyUserControl;
thisCombobox._LeftBorderChildChanged(e);
}
public void _LeftBorderChildChanged(DependencyPropertyChangedEventArgs e)
{
this.LeftBorder.Child = (UIElement)e.NewValue;
}
我们已经完成了这个类。让我们从其他类中调用它并在其中添加一个控件。
<local:MyUserControl Width="312" HorizontalAlignment="Right"
Margin="48, 0, 0, 0" VerticalAlignment="Center"
Height="56" >
<local:MyUserControl.LeftBorder>
<-- You can insert another control here -->
<-- Just don't remember that if you want to add more than one controls, you should add a panel then add controls into inside of the panel because Border child can only 1 child item -->
<StackPanel>
<-- Now you can insert your controls -->
</StackPanel>
</local:MyUserControl.LeftBorder>
</local:MyUserControl>
注意:当您首次执行此操作时,您必须在查看xaml设计器之前运行程序。运行程序后,所有设计系统将同步运行。
我希望我理解了您的意思并正确回答了您的问题。
谢谢。