将数据通过XAML传递到VB

时间:2015-01-15 21:45:51

标签: wpf vb.net xaml custom-controls

ORIGINAL(克莱门斯解决):

我有代码(下面),我试图通过XAML将参数传递给VB。这会给它一个错误代码" ' tabText'属性已经由' customTabPanel&#39> " 注册,我不知道我应该在这做什么,因为这是我第一次尝试解决这个问题

NEW:

这仍然没有正确传递文本,我不明白为什么。任何帮助将不胜感激。

VB:

Public Class customTabPanel
Inherits Grid
Dim workSpaceAssociation As Grid
Public ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("tabText", GetType(String), GetType(customTabPanel), New PropertyMetadata(String.Empty))
Public Property tabText() As String
    Get
        Return DirectCast(GetValue(TextProperty), String)
    End Get
    Set(value As String)
        SetValue(TextProperty, value)
    End Set
End Property
Sub New()
    Me.Height = 20
    Me.Background = New SolidColorBrush(Color.FromRgb(27, 27, 28))
    Dim textBlock As New TextBlock
    textBlock.Foreground = New SolidColorBrush(Colors.White)
    textBlock.Text = tabText
    textBlock.Width = 100
    textBlock.Padding = New Thickness(10, 0, 25, 0)
    Dim closeWorkspace As New TextBlock
    closeWorkspace.HorizontalAlignment = Windows.HorizontalAlignment.Right
    closeWorkspace.Text = ""
    closeWorkspace.Foreground = New SolidColorBrush(Colors.White)
    closeWorkspace.Height = 15
    closeWorkspace.Width = 15
    closeWorkspace.FontFamily = New FontFamily("Segoe UI Symbol")
    'add'
    Me.Children.Add(textBlock)
    Me.Children.Add(closeWorkspace)
    Me.Width = textBlock.Width
    Me.Margin = New Thickness(5, 0, 0, 0)
    Me.HorizontalAlignment = Windows.HorizontalAlignment.Left
End Sub
Sub SetText(ByVal t As String)
    tabText = t
End Sub
Function GetText() As String
    Return tabText
End Function
End Class

XAML:

<ThisIsAwsome:customTabPanel tabText="Start Screen" />

1 个答案:

答案 0 :(得分:1)

依赖项属性字段必须声明为Shared

Public Shared ReadOnly TextProperty As DependencyProperty = ...

为了获得有关更新的属性值的通知,您还必须使用属性的元数据注册PropertyChangedCallback:

Public Shared ReadOnly TextProperty As DependencyProperty =
    DependencyProperty.Register(
        "tabText", GetType(String), GetType(customTabPanel),
         New PropertyMetadata(
             String.Empty, New PropertyChangedCallback(AddressOf TabTextChanged)))

TabTextChanged回调中,您可以设置TextBlock的Text属性(不知道它是否有效的VB):

Private Shared Sub TabTextChanged(
    ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim panel As customTabPanel = CType(d, customTabPanel)
        panel.textBlock.Text = CType(e.NewValue, String)
End Sub

您可以在MSDN上的Custom Dependency Properties文章中找到详细说明。