DataTemplate 中的网格 ColumnDefinition.Width
在我的项目中,我创建了一个继承自 Control
的控件。它称为 DialogHeader
,顾名思义,用于在模态、不可调整大小的对话框上显示标题。事实上,它默认绑定到其父 Window
。该控件有一个名为 IconLocation
的属性,即图像是否应显示在控件标签的左侧或右侧:
[Image] [Label] -- or -- [Label] [Image]
与 DialogHeader
一起使用的模板基本上如下:
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="COLN_Left" Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition x:Name="COLN_Right" Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Name="PART_Image" />
<Separator Grid.Column="1"
Visibility="Collapsed" Width="{TemplateBinding SpacerWidth}" />
<TextBlock Grid.Column="2" Name="PART_Text" />
</Grid>
<ControlTemplare.Triggers>
<Trigger Property="ImageLocation" Value="Right">
<Setter TargetName="PART_Image" Property="Grid.Column" Value="2" />
<Setter TargetName="PART_Text" Property="Grid.Column" Value="0" />
<!-- The following doesn't work! Help! -->
<Setter TargetName="COLN_Left" Property="Width" Value="*" />
<Setter TargetName="COLN_Right" Property="Width" Value="auto" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
简单来说,当ImageLocation
属性设置为Location.Right
时,COLN_Left
和COLN_Right
的宽度应该是交换了。因此,我应该使用 [*][auto][auto]
,而不是 [auto][auto][*]
。
如何通过 ControlTemplate
实现此功能?如果没有,是否有不涉及使用 C# 代码的方法?
先感谢您。
In my project, I've made a control that inherits from Control
. It is called DialogHeader
and, as its name stands, is for displaying a header on modal, non-resizable dialogs. In truth, it binds by default to its parent Window
. The control has a property called IconLocation
, i.e. whether the image should be displayed on the left or right side of the control's label:
[Image] [Label] -- or -- [Label] [Image]
The template used with DialogHeader
is basically the following:
<ControlTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="COLN_Left" Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition x:Name="COLN_Right" Width="*" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Name="PART_Image" />
<Separator Grid.Column="1"
Visibility="Collapsed" Width="{TemplateBinding SpacerWidth}" />
<TextBlock Grid.Column="2" Name="PART_Text" />
</Grid>
<ControlTemplare.Triggers>
<Trigger Property="ImageLocation" Value="Right">
<Setter TargetName="PART_Image" Property="Grid.Column" Value="2" />
<Setter TargetName="PART_Text" Property="Grid.Column" Value="0" />
<!-- The following doesn't work! Help! -->
<Setter TargetName="COLN_Left" Property="Width" Value="*" />
<Setter TargetName="COLN_Right" Property="Width" Value="auto" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Simply put, when the ImageLocation
property is set to Location.Right
, the widths of COLN_Left
and COLN_Right
should be exchanged. So instead of [auto][auto][*]
, I should have [*][auto][auto]
.
How can I make this work from the ControlTemplate
? If not, is there are way that doesn't involve using C# code?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这部分确实有效,但我认为这可能不是您想要的。尝试将其删除,结果应该会有所不同。
That part does work, it possibly is just not what you want i presume. Try to remove it and the result should be different.