【问题标题】:Why this UserControl can't fill the TreeViewItem.Header?为什么这个 UserControl 不能填充 TreeViewItem.Header?
【发布时间】:2019-11-01 10:16:27
【问题描述】:

如上图,TreeViewItem.Header 宽度填充到父级。

enter image description here

但是它的子“Bd[Border]”太小了。为什么它不填充父默认值。

我该怎么办?我希望将 UserControl“TaskHeader”填充到 TreeViewItem.Header。

用户控制代码。

enter image description here

我将项目发布到GitHubPastNodes 文件夹中的项目。

【问题讨论】:

  • 抱歉,我不能发布图片,因为需要至少 10 个声望。

标签: c# wpf .net-core


【解决方案1】:

发生这种情况的原因是TreeViewItem 的控件模板将Header 内容放在Grid.ColumnWidth="Auto" 中。

这里有几个选项:

  1. 提取TreeViewItem 的控制模板并对其进行编辑以满足您的需要。
  2. 在 TaskHeader 上设置宽度,如下所示:Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Grid}}" - 这有点小技巧,但可以。

【讨论】:

  • 谢谢你,我尝试将“Horizo​​ntalAlignment”和“Horizo​​ntalContentAlignment”设置为“Stretch”,没有任何改变。
  • 我用问题的确切原因和几个选项更新了解决方案。
  • 我尝试选项2,结果是TaskHeader的宽度是无限的。现在我正在尝试选项 1。感谢您的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2023-04-02
  • 2014-10-03
相关资源
最近更新 更多