【问题标题】:Accessing class properties in xaml访问 xaml 中的类属性
【发布时间】:2010-02-18 15:17:05
【问题描述】:

我目前已经创建了下面的类。出于某种原因,虽然我无法访问通过我的 xaml 样式创建的属性。

Public Class Ribbon : Inherits Button
    Private mpopDropdown As Popup

    Public Property Dropdown() As Popup
        Get
            Return mpopDropdown
        End Get
        Set(ByVal value As Popup)
            mpopDropdown = value
        End Set
    End Property

    ...

End Class


<Style TargetType="{x:Type s:Ribbon}">
    <Setter Property="Ribbon.Dropdown">

此时出现“Invalid PropertyDescriptor value”错误。

我可以做些什么来使这个属性可以访问?

编辑:我也尝试过创建DependencyProperty,因为我读过这可以解决我的问题,但似乎没有。

编辑 2:我试过了

Public Shared Readonly DropdownProperty as DependencyProperty = _
    DependencyProperty.Register("Dropdown",GetType(Popup),GetType(Ribbon), _
    New FrameworkPropertyMetadata(False))

Public Shared Readonly DropdownProperty as DependencyProperty = _
    DependencyProperty.Register("Dropdown",GetType(Popup),GetType(Ribbon), _
    New FrameworkPropertyMetadata(True))

但他们似乎也没有公开该属性。我还将该属性标记为&lt;Bindable(True)&gt;,但这似乎没有任何作用。

知道我做错了什么吗?

【问题讨论】:

    标签: .net wpf vb.net wpf-controls


    【解决方案1】:

    依赖属性确实可以解决您的问题。并且不要忘记命名空间,即&lt;Setter Property="s:Ribbon.Dropdown"&gt;,尽管如果您指定了TargetType,则不必在setter 中为属性指定类所有者。这意味着你可以写&lt;Setter Property="Dropdown"&gt;

    PS:您可能还会遇到通过样式设置视觉效果的另一个问题。 Read more。但那是另一回事了……

    【讨论】:

      【解决方案2】:

      我最终只是创建了一个单独的 Popup 样式并在类的代码中设置我的对象的样式并设置 Popup 的 PlacementTarget = Me

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-02
        • 1970-01-01
        • 2018-03-12
        • 2019-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-15
        相关资源
        最近更新 更多