使用动态资源通过样式设置值时未分配的依赖属性值

时间:2012-11-03 06:32:50

标签: .net wpf vb.net exception-handling dependency-properties

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:值不能为null。参数名称:property。

我的目标是从资源XAML控制TreeviewItem.IsExpanded属性。

1 个答案:

答案 0 :(得分:0)

您无法通过向现有依赖项属性ExpandAll添加所有者类型来创建新的依赖项属性IsExpanded

使用Register代替AddOwner注册新属性:

Public Shared ReadOnly ExpandAllproperty As DependencyProperty =
    DependencyProperty.Register(
        "ExpandAll", GetType(Boolean), GetType(UITreeView),
        New FrameworkPropertyMetadata(True, FrameworkPropertyMetadataOptions.Inherits))
相关问题