C# 如何为 GroupBox 标题使用样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15808446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use style for GroupBox header?
提问by Hossein Narimani Rad
I have lost of GroupBox
in my form that their header text must be Bold. I know how to do it for a single GroupBox
:
我GroupBox
在我的表单中丢失了他们的标题文本必须是Bold。我知道如何为一个人做到这一点GroupBox
:
<GroupBox>
<GroupBox.Header>
<TextBlock Text="HEADER TEXT" FontWeight="Bold"/>
</GroupBox.Header>
</GroupBox>
But I'm interested to know how to do it with Styles
. Here is what I have tried:
但我有兴趣知道如何使用Styles
. 这是我尝试过的:
<Style TargetType="GroupBox">
<Setter Property="BorderBrush" Value="{StaticResource lightBlueBrush}"/>
<Setter Property="Margin" Value="25,1,5,5"/>
//<Setter ??
</Style>
I have tried <Setter Property="HeaderTemplate" Value={StaticResource myTemp}>
Which myTemp
is a simple DataTemplate
But VS suddenly closed! I'm not sure if I'm in the correct way of doing it, so anyone could help me?
我试过了<Setter Property="HeaderTemplate" Value={StaticResource myTemp}>
哪个myTemp
简单DataTemplate
但是VS突然关闭了!我不确定我的做法是否正确,所以有人可以帮助我吗?
EDIT:Please test your idea before posting it as an answer!
编辑:请在将其发布为答案之前测试您的想法!
采纳答案by DHN
Did you try the following?
您是否尝试过以下操作?
<Style TargetType="GroupBox">
<Setter Property="BorderBrush" Value="{StaticResource lightBlueBrush}"/>
<Setter Property="Margin" Value="25,1,5,5"/>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" FontWeight="Bold"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
Usage:
用法:
<GroupBox Header="Title" />
回答by JSJ
A groupboxs headerTemplate is a type of DataTemplate. so you should provide a datatemplate object insteed of style or template.
groupboxs headerTemplate 是一种 DataTemplate。所以你应该提供一个数据模板对象而不是样式或模板。
try below one.
试试下面一个。
<Window.Resources>
<DataTemplate x:Key="DataTemplate1">
<TextBlock Text="Test Templated Header"/>
</DataTemplate>
</Window.Resources>
<Grid>
<GroupBox Header="Test Header" HeaderTemplate="{StaticResource DataTemplate1}">
<Border BorderBrush="Red" Margin="10">
<TextBlock Text="Hello"/>
</Border>
</GroupBox>
</Grid>