Binding Silverlight UserControl custom properties to its' elements

后端 未结 6 599
长情又很酷
长情又很酷 2021-02-05 09:59

I\'m trying to make a simple crossword puzzle game in Silverlight 2.0. I\'m working on a UserControl-ish component that represents a square in the puzzle. I\'m having trouble wi

6条回答
  •  醉话见心
    2021-02-05 10:56

    Try this:

    Public ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(ButtonEdit), New System.Windows.PropertyMetadata("", AddressOf TextPropertyChanged))
    Public Property Text As String
        Get
            Return GetValue(TextProperty)
        End Get
        Set(ByVal value As String)
            SetValue(TextProperty, value)
        End Set
    End Property
    Private Sub TextPropertyChanged()
        If String.IsNullOrEmpty(Text) Then
            TextBox1.Text = ""
        Else
            TextBox1.Text = Text
        End If
    End Sub
    Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TextBox1.LostFocus
        Text = TextBox1.Text
    End Sub
    

    I can bind in both XAML and code behind.

提交回复
热议问题