问题描述
我有一个像这样的 ContentControl
:
<ContentControl>
<userControls:TestControl/>
</ContentControl>
或者像这样[当我有 PRISM 系统时]:
<ContentControl prism:RegionManager.RegionName="TestView"/>
我看到最后的 UserControl
well 直到我启动程序的这一步.
I see the final UserControl
well until this step when i start the program.
在上述示例中,Content
类型是 UserControl
.现在我想给这个 ContentControl
一个 ControlTemplate
.然后我创建了一个名为 StyleTest
的 style
并在我的 Xaml 中使用它:
In the above samples the Content
type is UserControl
. Now i want give a ControlTemplate
to this ContentControl
. Then i created a style
named StyleTest
and used it in my Xaml:
<ContentControl Style="{StaticResource StyleTest}"> .....
我的风格:
<Style x:Key="StyleTest" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Margin="10">
<Border CornerRadius="10" BorderBrush="#ffffffff" BorderThickness="5">
<StackPanel>
<ContentPresenter Content="{Binding}"/>
<TextBlock>Some additional text to test template</TextBlock>
</StackPanel>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但是当我启动程序时,UserControl
无法被看到,我只能看到这个文本和它周围的边框:一些额外的文本来测试模板
But when i start the program the UserControl
can not be seen and i just see this text and a border around it: Some additional text to test template
- 我必须写什么而不是上面的代码行才能再次看到我的
UserControl
周围有白色边框? - 为什么
UserControl
没有显示在上面的代码中(上面的样式)?
- What i must write instead of above line of code to see my
UserControl
again with a white border around it? - Why the
UserControl
not showing with above code (above style)?
推荐答案
有 3 种方法可以做到这一点.
There are 3 ways to do this.
- by setting the ContentTemplate
- by setting the Template
- or using the Border directly and apply a style.
在这种情况下,我将使用 Border 并应用样式,因为看起来 ContentControl 仅用于添加样式的 Border.
In this case I would use the Border and apply a style because it looks like the ContentControl is only used to do add a styled Border.
<StackPanel>
<StackPanel.Resources>
<Style x:Key="BorderStyle" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="5" />
<Setter Property="CornerRadius" Value="10" />
<Setter Property="Margin" Value="10" />
</Style>
<Style x:Key="ContentTemplateStyle" TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Border Style="{StaticResource BorderStyle}">
<!-- Bind to the DataContext of the DataTemplate which is the Content of the ContentControl -->
<!-- <ContentPresenter Content="{Binding}" />-->
<!-- TemplateBinding improves performance -->
<ContentPresenter Content="{TemplateBinding Content}" />
<!-- Using the TemplatedParent -->
<!--<ContentPresenter Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>-->
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ControlTemplateStyle" TargetType="{x:Type ContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Border Style="{StaticResource BorderStyle}">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Resources>
<ContentControl Style="{StaticResource ContentTemplateStyle}">
<Button>ContentTemplateStyle</Button>
</ContentControl>
<ContentControl Style="{StaticResource ControlTemplateStyle}">
<Button>ControlTemplateStyle</Button>
</ContentControl>
<Border Style="{StaticResource BorderStyle}">
<Button>BorderStyle</Button>
</Border>
</StackPanel>
这篇关于当内容类型为 UserControl 时,WPF 为 ContentControl 编写 ControlTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!