Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在UserControl实例中嵌入内容_Wpf - Fatal编程技术网

Wpf 在UserControl实例中嵌入内容

Wpf 在UserControl实例中嵌入内容,wpf,Wpf,我有一个wpfusercontrol,它只是它附带的任何东西的标签。例如,文本框的标签。我想将此文本框放置在LabeledControl标记中,如下所示: <LabeledControl Label="First name"> <TextBox Binding="{FirstName}" /> </LabeledControl> 我之所以要这样做,是为了设计控件及其标签的外观 我找不到一个明显的方法来做这件事。我这样做对吗?是否应该改为查看模板?我认

我有一个wpfusercontrol,它只是它附带的任何东西的标签。例如,文本框的标签。我想将此文本框放置在LabeledControl标记中,如下所示:

<LabeledControl Label="First name">
  <TextBox Binding="{FirstName}" />
</LabeledControl>

我之所以要这样做,是为了设计控件及其标签的外观


我找不到一个明显的方法来做这件事。我这样做对吗?是否应该改为查看模板?

我认为更好的选择是使用内置的
HeaderedContentControl
,它允许您指定
标题
(您的标签)和
内容
(您的文本框)属性

然后,您可以为
HeaderedContentControl
指定一个
ControlTemplate
,以更改外观:

<Style x:Key="MyLabelledItemStyle" TargetType="HeaderedContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="HeaderedContentControl">
                <StackPanel Orientation="Horizontal">
                    <ContentControl Content="{TemplateBinding Header}" Margin="2" />
                    <ContentControl Content="{TemplateBinding Content}" Margin="2" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<HeaderedContentControl Style="{StaticResource MyLabelledItemStyle}" Header="First Name">
    <TextBox Text="{Binding FirstName}" />
</HeaderedContentControl>