【问题标题】:Set treeview virtualization to true by default in a custom control在自定义控件中默认将树视图虚拟化设置为 true
【发布时间】:2013-07-05 20:43:39
【问题描述】:

有没有办法将从树视图派生的自定义控件的虚拟化默认设置为 true?例如,我有一个从 RadTreeView 派生的自定义控件:

public class MyTreeViewControl : RadTreeView
{
    static MyTreeViewControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyTreeViewControl), new FrameworkPropertyMetadata(typeof(MyTreeViewControl)));
    }
}

还有一个包含以下内容的 Generic.Xaml 文件:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:userControls="clr-namespace:HDC.Solus.WPF.UserControls"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:classes="clr-namespace:HDC.Solus.WPF.Classes">

<HierarchicalDataTemplate DataType="{x:Type classes:MyTreeViewItem}" ItemsSource="{Binding Children}">
    <StackPanel Orientation="Horizontal">
        <Image Height="16" Width="16" Source="{Binding IconUri}" />
        <TextBlock Text="{Binding Text}" />
    </StackPanel>
</HierarchicalDataTemplate>

<Style TargetType="{x:Type userControls:MyTreeViewControl}" BasedOn="{StaticResource {x:Type telerik:RadTreeView}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type userControls:MyTreeViewControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="telerik:TreeViewPanel.IsVirtualizing" Value="True"/>
</Style>

但是,默认情况下不启用虚拟化,启用虚拟化的唯一方法是在创建时直接在控件上进行设置,例如:

<userControls:MyTreeViewControl IsVirtualizing="True" />

【问题讨论】:

  • 只是好奇...为什么它是 Setter Property="telerik:TreeViewPanel.IsVirtualizing"?在样式中使用 VirtualizingStackPanel.IsVirtualizing="True" 不起作用吗?
  • 我试过了,但给出了错误“属性'IsVirtualizing'不可附加到'ResourceDictionary'类型的元素”。上面的代码在创建自定义控件时创建的 Generic.Xaml 中。
  • 你能发布你的 MyTreeViewControl 吗?
  • 我已经更新了主要问题,将代码包含在两个文件中,干杯...

标签: wpf treeview virtualization


【解决方案1】:

我没有使用太多...但是用您选择的一个替换 Treeviewpanel 或某些模板不起作用吗?如果不能在 xaml 中设置。

如果您想覆盖控件的默认元数据,您可以在构造函数中执行类似的操作。不知道会不会有帮助

TreeView.ItemsPanelProperty.OverrideMetadata(typeof(MyTreeViewControl), new PropertyMetadata(myNewItemsPanel));

这是您通常如何更改继承对象的依赖属性的方式。

您可能还想在自定义控件中创建一个“IsVirtualizing 依赖属性”,然后在“OnApplyTemplate”中将该属性绑定到 telerik:TreeViewPanel.IsVirtualizing 属性。或者将 XAML 中的 telerik:TreeViewPanel.IsVirtualizing 属性绑定到新created 是虚拟化依赖属性。

希望你能解决:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    相关资源
    最近更新 更多