具有不同控件模板的主题 WPF TreeViewItems

发布于 2024-11-04 02:20:26 字数 532 浏览 1 评论 0原文

我正在尝试为继承自 TreeView 的自定义 PropertyGrid 控件创建不同的主题,并使用 TreeViewItems 作为项目容器。此外,我想根据 TreeViewItem 绑定到的对象类型设置 TreeViewItems 的控件模板。我更愿意通过设置 ItemsSource 来填充 PropertyGrid,就像现在一样。

目前,我通过 HierarchialDataTemplate 设置 TreeViewItems.ItemContainerStyleSelector,根据绑定对象的类型应用不同的控件模板。在此样式选择器中,我通过资源键返回静态资源。我不想使用此选择器和资源键方法,而是根据要绑定的对象的类型创建不同的派生 TreeViewItem 对象。这将允许我根据类型为每个主题应用不同的样式,就像其他控件的主题一样。但我看不出有什么办法可以做到这一点。如果覆盖 ItemsControl.GetContainerForItemOverride 传递了要绑定到的对象,或者至少是其类型,则它会执行我想要的操作,但我没有看到一种方法可以实现此目的。关于如何做到这一点有什么想法吗?或者,我对他的看法是错的吗?

I am trying to create different themes for a custom PropertyGrid control that inherits from TreeView, and uses TreeViewItems as the item containers. Additionally, I want to set the control template for the TreeViewItems based on the type of object the TreeViewItem is bound to. I would prefer to populate the PropertyGrid as I am now by setting ItemsSource.

Currently I am applying different control templates based on the type of bound object by setting the TreeViewItems.ItemContainerStyleSelector via a HierarchialDataTemplate. In this style selector I am returning a static resource via a resource key. I would like to not use this selector and resource key method, but rather create different derived TreeViewItem objects based on the type of the object it is to be bound to. This would allow me to apply a different style per theme based on type, in the same way that other controls are themed. But I don’t see a way to do this. The override ItemsControl.GetContainerForItemOverride would do what I want if it was passed the object to be bound to, or at least its type, but I don’t see a way to make this work. Any ideas on how to do this? Or, am I going about his all wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

回眸一笑 2024-11-11 02:20:26

最好的选择是将只读自定义依赖项属性添加到自定义 TreeViewItem。由于没有更好的名字,我们称之为“Look”。该属性可以是包含您想要应用的各种外观或样式的枚举。

然后,您可以为自定义 TreeViewItem 创建单个样式,它使用基于“外观”属性的样式触发器来更改模板或各种其他属性。

然后,您可以在 PrepareContainerForItemOverride< 中设置 Look 属性/a>,基于给定的项目。

ItemsControl 通常期望它们的“容器”是单一类型,但这并没有严格执行。但正如您所说,您无法根据其包装的物品创建不同的容器。

此外,虚拟化还增加了一些复杂性。在这种情况下,容器可能会被重复使用,因此您将没有机会“创建”新的属性类型。但是,即使启用了虚拟化,PrepareContainerForItemOverride 也会被调用。

Your best bet would be to add a read-only custom dependency property to your custom TreeViewItem. For lack of a better name, let's call this "Look". This property can then be an enumeration with the various looks or Styles you want applied.

Then you can create a single Style for your custom TreeViewItem, which uses Style Triggers based on your Look property to change the Template or various other properties.

You can then set your Look property in a PrepareContainerForItemOverride, based on the item that is given.

The ItemsControl in general expect their "container" to be of a single type, but this isn't strictly enforced. But as you said, you can't create different containers based on the item it is wrapping.

In addition, virtualization adds a bit of complexity to the mix. As in that case, containers may be reused so you would not have a chance to "create" a new one of the property type. But, even with virtualization enabled, the PrepareContainerForItemOverride will be called.

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