DataTemplate 中的网格 ColumnDefinition.Width

发布于 2024-12-14 03:09:25 字数 1711 浏览 4 评论 0原文

在我的项目中,我创建了一个继承自 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_LeftCOLN_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2024-12-21 03:09:25

这部分确实有效,但我认为这可能不是您想要的。尝试将其删除,结果应该会有所不同。

That part does work, it possibly is just not what you want i presume. Try to remove it and the result should be different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文