Wpf 关于扩展现有样式的新控件结构的问题
我正在尝试使用该样式进行重用。 我看到的文章如下 本文展示了如何扩展控件以使用现有样式 当通过扩展现有样式创建新控件时,我开始对样式结构感到好奇 例如,在本文中,定义了具有“Style\u HeaderedContentControl”键的HeaderedContentControl,如下所示Wpf 关于扩展现有样式的新控件结构的问题,wpf,styles,Wpf,Styles,我正在尝试使用该样式进行重用。 我看到的文章如下 本文展示了如何扩展控件以使用现有样式 当通过扩展现有样式创建新控件时,我开始对样式结构感到好奇 例如,在本文中,定义了具有“Style\u HeaderedContentControl”键的HeaderedContentControl,如下所示 <Style x:Key="Style_HeaderedContentControl" TargetType="{x:Type HeaderedContentControl}"> &
<Style x:Key="Style_HeaderedContentControl" TargetType="{x:Type HeaderedContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<Border Background="WhiteSmoke"
BorderBrush="DarkGray"
BorderThickness="1"
Margin="5">
<StackPanel Background="WhiteSmoke"
Margin="5">
<ContentPresenter ContentSource="Header"
TextBlock.FontWeight="Bold"
Margin="0,0,0,5"/>
<ContentPresenter />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<HeaderedContentControl Header="재생" Grid.Column="1"
Style="{DynamicResource Style_HeaderedContentControl}">
<StackPanel>
<Button Content="시작"/>
<Button Content="정지"/>
</StackPanel>
</HeaderedContentControl>
并通过扩展上述样式创建一个新控件,如下所示
<Style x:Key="Style_HeaderedContentControl" TargetType="{x:Type HeaderedContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type HeaderedContentControl}">
<Border Background="WhiteSmoke"
BorderBrush="DarkGray"
BorderThickness="1"
Margin="5">
<StackPanel Background="WhiteSmoke"
Margin="5">
<ContentPresenter ContentSource="Header"
TextBlock.FontWeight="Bold"
Margin="0,0,0,5"/>
<ContentPresenter />
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<HeaderedContentControl Header="재생" Grid.Column="1"
Style="{DynamicResource Style_HeaderedContentControl}">
<StackPanel>
<Button Content="시작"/>
<Button Content="정지"/>
</StackPanel>
</HeaderedContentControl>
在这里,我想知道HeaderedContentControl结构是怎样的
我认为候选人包括:
<Border Background="WhiteSmoke"
BorderBrush="DarkGray"
BorderThickness="1"
Margin="5">
<StackPanel Background="WhiteSmoke"
Margin="5">
<ContentPresenter ContentSource="Header"
TextBlock.FontWeight="Bold"
Margin="0,0,0,5"/>
<StackPanel>
<Button Content="시작"/>
<Button Content="정지"/>
</StackPanel>
<ContentPresenter />
</StackPanel>
</Border>
或
或
正确答案是什么?你能告诉我为什么要这样吗
感谢阅读。当您这样做时
<HeaderedContentControl Header="재생" Grid.Column="1"
Style="{DynamicResource Style_HeaderedContentControl}">
<StackPanel>
<Button Content="시작"/>
<Button Content="정지"/>
</StackPanel>
</HeaderedContentControl>
这表明:
现在让我们为它声明一个模板,并使用template=“{StaticResource MyTemplate}”
设置它:
现在,您已经完全更改了控件的整体外观,但您的内容显示在您使用
指定的位置:
谢谢你的详细解释,因为你的例子很简单,所以我能理解。但在我的例子中,有些东西我还不明白。我打算写一篇关于它的新文章,因为这篇评论无法解释。我希望你能回答这篇文章。
<ContentPresenter />
<ContentControl HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="This is the content" Background="CornflowerBlue" />
</ContentControl>
<ControlTemplate x:Key="MyTemplate" TargetType="{x:Type ContentControl}">
<GroupBox Header="This is the template">
<ContentPresenter />
</GroupBox>
</ControlTemplate>