WPF - 在 UserControl 中托管内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10427133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - Hosting content inside a UserControl
提问by Erez
I'm trying to create a user control that has a Grid
with two rows.
the first row for a title and the second one for a content that will be defined outside the user control such as a Button
in our example.
我正在尝试创建一个Grid
具有两行的用户控件。第一行用于标题,第二行用于将在用户控件之外定义的内容,例如Button
我们示例中的 a。
Somehow I didn't get it to work.
不知何故,我没有让它发挥作用。
UserControl1 xaml:
用户控件 1 xaml:
<Grid Background="LightBlue">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/>
</Grid>
MainWindow xaml:
主窗口 xaml:
<Grid>
<local:UserControl1>
<Button>Click me</Button>
</local:UserControl1>
</Grid>
The picture below should explain what's my problem:
下面的图片应该可以解释我的问题:
回答by EvAlex
The following code
以下代码
<local:UserControl1>
<Button>Click me</Button>
</local:UserControl1>
Means that you set UserControl1
's Content property to be that button. This button simply replaces that UserControls1
's markup. So all the things that you have in UserControl1.xaml are not there any more.
意味着您将UserControl1
的 Content 属性设置为该按钮。这个按钮只是替换了那个UserControls1
标记。因此,您在 UserControl1.xaml 中拥有的所有内容都不再存在。
EDIT
编辑
If you want your UserControl to host some markup that will be set somewhere outside of it, you can add a DependencyProperty
to it, for example:
如果您希望您的 UserControl 承载一些将在其外部某处设置的标记,您可以向其添加一个DependencyProperty
,例如:
/// <summary>
/// Gets or sets additional content for the UserControl
/// </summary>
public object AdditionalContent
{
get { return (object)GetValue(AdditionalContentProperty); }
set { SetValue(AdditionalContentProperty, value); }
}
public static readonly DependencyProperty AdditionalContentProperty =
DependencyProperty.Register("AdditionalContent", typeof(object), typeof(UserControl1),
new PropertyMetadata(null));
And add some element to it's markup to host that additional content. Here's an example extending the markup you provided:
并向其标记添加一些元素以托管该附加内容。这是扩展您提供的标记的示例:
<UserControl ... Name="userControl">
<Grid Background="LightBlue">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/>
<ContentPresenter Content="{Binding AdditionalContent, ElementName=userControl}" />
</Grid>
</UserControl>
Now you can use it as following:
现在您可以按如下方式使用它:
<local:UserControl1>
<local:UserControl1.AdditionalContent>
<Button>Click me</Button>
</local:UserControl1.AdditionalContent>
</local:UserControl1>
回答by blindmeis
You have to set the ControlTemplate
:
你必须设置ControlTemplate
:
<UserControl>
<UserControl.Resources>
<Style TargetType="{x:Type local:UserControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:UserControl1}">
<Grid Background="LightBlue">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Title" FontSize="30" Margin="10,0,0,0"/>
<ContentPresenter Grid.Row="1" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
</UserControl>
回答by Alamakanambra
Use template with
使用模板
< ContentControl />
< 内容控制 />
Instead of using Content Presenter
而不是使用内容展示器
So place this:
所以放置这个:
<UserControl.Style>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type UserControl}" >
<Grid Background="LightBlue">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/>
<ContentControl Grid.Row="1" Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Style>
to your userControl
到您的用户控件
回答by codekaizen
You can template the user control to add additional visuals like the TextBlock
.
您可以模板用户控件以添加其他视觉效果,例如TextBlock
.
<UserControl>
<UserControl.Style>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Background="LightBlue">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/>
<ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Style>
<Button>
Click me!
</Button>
</UserControl>