WPF - 托管UserControl中的内容

Ere*_*rez 51 wpf user-controls

我正在尝试创建一个Grid包含两行的用户控件.标题的第一行和第二行的内容将在用户控件之外定义,例如Button在我们的示例中.

不知何故,我没有让它工作.

UserControl1 xaml:

  <Grid Background="LightBlue">
    <Grid.RowDefinitions>
        <RowDefinition Height="50" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <TextBlock Text="Title" FontSize="30" Margin="10,0,0,0"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

MainWindow xaml:

 <Grid>
    <local:UserControl1>
        <Button>Click me</Button>
    </local:UserControl1>
</Grid>
Run Code Online (Sandbox Code Playgroud)

下面的图片应该解释我的问题: 在此输入图像描述

EvA*_*lex 64

以下代码

<local:UserControl1>
    <Button>Click me</Button>
</local:UserControl1>
Run Code Online (Sandbox Code Playgroud)

表示您将UserControl1Content属性设置为该按钮.这个按钮只是替换了那个UserControls1标记.所以你在UserControl1.xaml中拥有的所有东西都不再存在了.

编辑

如果您希望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));
Run Code Online (Sandbox Code Playgroud)

并在其标记中添加一些元素以托管其他内容.这是扩展您提供的标记的示例:

<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>
Run Code Online (Sandbox Code Playgroud)

现在您可以使用它如下:

<local:UserControl1>
    <local:UserControl1.AdditionalContent>
        <Button>Click me</Button>
    </local:UserControl1.AdditionalContent>
</local:UserControl1>
Run Code Online (Sandbox Code Playgroud)

  • 您可以在类上使用`[ContentProperty("AdditionalContent")]`属性,而不是使用`<local:UserControl1.AdditionalContent>`,然后可以在控件的内容中包含自定义控件.这很好,因为没有人可以通过意外设置内容而不是"AdditionalContent"来破坏它. (26认同)
  • 什么是“ CalibrationPoint”?智慧无法为该类找到可能的解决方法,这是一些习惯吗?[[编辑]]对不起,我没有检查方法`Register`参数。我认为应该将其更改为`UserControl1`进行编译。 (3认同)

bli*_*eis 24

你必须设置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>
Run Code Online (Sandbox Code Playgroud)

  • 是否有理由通过`UserControl.Resources/Style`设置它而不是仅仅将`ControlTemplate`作为`UserControl.Template`的内容? (8认同)
  • UserControl.Template 对我有用。我使用 TargetType="{x:Type UserControl}"。 (2认同)

Ala*_*bra 9

使用模板

<ContentControl />

而不是使用Content Presenter

所以放置这个:

<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>
Run Code Online (Sandbox Code Playgroud)

到你的userControl

  • *将模板与 ContentControl 一起使用,而不是使用 ContentPresenter* - 为什么? (3认同)
  • 最好在模板内使用 ContentPresenter,请参阅http://stackoverflow.com/a/1288353/991267 (3认同)

cod*_*zen 6

您可以模板化用户控件以添加其他视觉效果,例如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>
Run Code Online (Sandbox Code Playgroud)