0
Public Class UITreeview
    Inherits System.Windows.Controls.TreeView

Public Shared ExpandAllproperty As DependencyProperty
Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(UITreeview), New FrameworkPropertyMetadata(GetType(UITreeview)))
        UITreeview.ExpandAllproperty = UITreeViewItem.IsExpandedProperty.AddOwner(GetType(UITreeview), New FrameworkPropertyMetadata(True, FrameworkPropertyMetadataOptions.Inherits))
    End Sub
Public Property ExpandAll As Boolean
        Get
            Return Me.GetValue(ExpandAllproperty)
        End Get
        Set(value As Boolean)
            Me.SetValue(ExpandAllproperty, value)
        End Set
    End Property 
....
End Class

我创建了自己的依赖属性,我使用样式设置

<Style  TargetType="{x:Type UINat:UITreeview}">
    <Setter Property="ExpandAll" Value="False" />
</Style>

但不幸的是,我遇到了一个错误:

System.ArgumentNullException:值不能为空。参数名称:属性。

我的目标是控制来自 Resource XAML 的 TreeviewItem.IsExpanded 属性。

4

1 回答 1

0

您不能通过ExpandAll将所有者类型添加到现有依赖项属性来创建新的依赖项属性IsExpanded

使用Register而不是 AddOwner 来注册新属性:

Public Shared ReadOnly ExpandAllproperty As DependencyProperty =
    DependencyProperty.Register(
        "ExpandAll", GetType(Boolean), GetType(UITreeView),
        New FrameworkPropertyMetadata(True, FrameworkPropertyMetadataOptions.Inherits))
于 2012-11-03T15:18:48.057 回答