gpt4 book ai didi

当内容类型为 UserControl 时,WPF 为 ContentControl 编写 ControlTemplate

转载 作者:行者123 更新时间:2023-12-02 01:34:29 25 4
gpt4 key购买 nike

我有一个 ContentControl像这样:

<ContentControl>
<userControls:TestControl/>
</ContentControl>

或者像这样[当我有 PRISM 系统时]:

<ContentControl prism:RegionManager.RegionName="TestView"/>

我看到最后一个 UserControl 好吧直到我启动程序的这一步。

在上面的示例中,Content类型是 UserControl .现在我想给一个ControlTemplate对此 ContentControl .然后我创建了一个 style名为 StyleTest并在我的 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看不到,我只看到这个文本和它周围的边框:Some additional text to test template

  • 我必须写什么而不是上面的代码行才能看到我的 UserControl还是周围有白色边框?
  • 为什么 UserControl没有显示上面的代码(上面的样式)?

最佳答案

有 3 种方法可以做到这一点。

- by setting the ContentTemplate
- by setting the Template
- or using the Border directly and apply a style.

这种情况下,我会使用 Border 并应用样式,因为看起来 ContentControl 仅用于添加样式化的 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32029976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com