[英]Cannot set Header of GroupBox when ControlTemplate applied
I am using the following control template 我正在使用以下控制模板
<ControlTemplate x:Key="CriterionTemplate" Name="CriterionTemplate">
<GroupBox Name="CriterionGroupbox">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="50"/>
</Grid.ColumnDefinitions>
<Button x:Name="KeywordSelectionButton" Grid.Column="0" Height="50" IsManipulationEnabled="True" Style="{StaticResource HiddenButtonStyle}">
<DockPanel HorizontalAlignment="Left" Width="{TemplateBinding ActualWidth}">
<Image Name="SelectionImage" HorizontalAlignment="Left" Height="50"/>
<Label Name="SelectionLabel" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="11" FontFamily="Comic Sans MS"/>
</DockPanel>
</Button>
<Button x:Name="ClearButton" Grid.Column="2" Height="50"/>
</Grid>
</GroupBox>
</ControlTemplate>
In my MainWindow code behind, I dynamically create a variable number of controls having this template 在后面的MainWindow代码中,我动态创建了具有此模板的可变数量的控件
var template = this.FindResource("CriterionTemplate") as ControlTemplate;
GroupBox gb = new GroupBox()
{
Template = template,
Header = "MyCustomHeader",
};
gb.ApplyTemplate();
I create a bunch of those groupboxes and add them to some stackpanel. 我创建了一堆这些groupbox,并将它们添加到某些stackpanel中。 So far so good.
到现在为止还挺好。 Problem now is that the Header attribute of the groupbox is not set .
现在的问题是未设置 groupbox的Header属性。 Its just blank.
它只是空白。 The groupbox itsself with the template looks perfect, just the Header is empty.
带有模板的groupbox本身看起来很完美,只是Header为空。 Once I remove the template, the Header is displayed correctly.
删除模板后,标题将正确显示。
What do I do wrong? 我做错了什么?
In your template, you haven't specified how and where your Header
will be displayed. 在模板中,您尚未指定
Header
显示方式和显示位置。
Once you decide to show Header in a Label, you need to 决定在标签中显示标题后,您需要
Set TargetType
of Control Template to GroupBox
. 将控制模板的
TargetType
设置为GroupBox
。 This will make sure that TemplateBinding of GroupBox
properties work. 这将确保
GroupBox
属性的TemplateBinding有效。
You have to do TemplateBinding like this 你必须像这样做TemplateBinding
<Label Content = "{TemplateBinding Header}">
Got the solution by myself. 我自己找到了解决方案。
There was a structural problem within the code itsself. 代码本身存在结构性问题。 I defined two GroupBoxes.
我定义了两个GroupBox。 One within the template, one outside in the code-behind.
模板中的一个,代码后面的一个。
To modify the Header of the outer GroupBox in the code-behind did nor reflect the UI, however modifying the inner one which is defined in the template DID. 修改后面的代码中的外部GroupBox的Header并没有反映UI,但是修改了模板DID中定义的内部的。
Anyway I have to re-think my design here. 无论如何,我必须在这里重新考虑我的设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.