这里是做你想要什么的一种方式的示例:
首先,代码 - UserControl1.xaml.cs
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty MyContentProperty =
DependencyProperty.Register("MyContent", typeof(object), typeof(UserControl1));
public UserControl1()
{
InitializeComponent();
}
public object MyContent
{
get { return GetValue(MyContentProperty); }
set { SetValue(MyContentProperty, value); }
}
}
而且用户控件的XAML - UserControl1.xaml
<UserControl x:Class="InCtrl.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" Name="MyCtrl">
<StackPanel>
<Button Content="Up"/>
<ContentPresenter Content="{Binding ElementName=MyCtrl, Path=MyContent}"/>
<Button Content="Down"/>
</StackPanel>
</UserControl>
最后,XAML使用我们美好的新控制:
<Window x:Class="InCtrl.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:me="clr-namespace:InCtrl"
Title="Window1" Height="300" Width="300">
<Grid>
<me:UserControl1>
<me:UserControl1.MyContent>
<Button Content="Middle"/>
</me:UserControl1.MyContent>
</me:UserControl1>
</Grid>
</Window>
来源
2009-06-09 14:56:34
Nir
接受的答案什么问题呢?这听起来很容易...... – Tony 2009-06-09 12:39:23
问题不在于收集和导航按钮,而在于我希望在另一个应用程序中使用它时,可以将控件添加到用户控件中。 – 2009-06-09 12:52:28
如果我尝试在和之间放置多个控件,那么我只能在其中放置一个控件。如果我尝试一个网格,我的自定义控件上的控件就会消失。 –
2009-06-09 13:20:05