0

我在 uwp 应用程序中使用WinrtXamlToolkit中的TreeView控件。我想根据条件对某些 TreeViewItems 应用不同的样式,因此我创建了一个派生自 StyleSelector 的类 TreeViewItemStyleSelector 并覆盖了 SelectStyleCore 函数。

 public Style ResourceStyle { get; set; }

 public Style ClassroomStyle { get; set; }

 protected override Style SelectStyleCore(object item, DependencyObject container)
        {
            // control never reaches here. 
            // logic to apply style
        }

然后在xaml中我像这样使用它。

在页面资源

<StyleSelectors:TreeViewItemStyleSelector ResourceStyle="{StaticResource AStyle}" ClassroomStyle = "{StaticResource BStyle}"/> 

稍后在页面中。

<wxtControls:TreeView ItemsSource="{Binding StructureViewModels}" ItemContainterStyleSelector="{StaticResource TreeViewItemStyleSelector}" />

问题是永远不会调用 SelectStyleCore 覆盖。有人知道为什么吗?

4

1 回答 1

0

尽管我有一些理论,但我还不确定这不起作用的原因是什么。一个是 - 这从未实施过。也许至少在根级别它应该工作,因为它是一个ItemsControl,但由于它的实现方式(分层) -ItemContainerStyleSelector必须从 转发TreeViewTreeViewItems,但事实并非如此。

我还没有机会尝试复制它,但是如果我要尝试修复它或解决它 - 我会首先尝试将该属性转发到HeaderedItemsControl.cs- 大致是它所说的“ // Note: this is where we would apply the HeaderTemplateSelector (...) (if implemented)”。另一种方法(如果您不想修改工具包的代码)可能是在模板中为TreeViewItemand 指定模板 - 使用StyleSelector您希望不同数据项不同的模板部分。

于 2016-07-24T06:14:08.523 回答