好吧 - 我把頭髮拉出我認爲是一個簡單的場景:爲包含兩個附加屬性(EnglishText,FrenchText)的雙語使用創建自定義標籤。目前,它的結構是這樣的:WPF自定義控件並通過DependencyProperty公開屬性
Public Class myCustomLabel
Inherits System.Windows.Controls.Label
Public myEnglishTextProperty As DependencyProperty = DependencyProperty.Register("myEnglishText", GetType(String), GetType(myCustomLabel), New PropertyMetadata("English", New PropertyChangedCallback(AddressOf TextChanged)))
Public myFrenchTextProperty As DependencyProperty = DependencyProperty.Register("myFrenchText", GetType(String), GetType(myCustomLabel), New PropertyMetadata("Francais", New PropertyChangedCallback(AddressOf TextChanged)))
Public Sub New()
'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
'This style is defined in themes\generic.xaml
DefaultStyleKeyProperty.OverrideMetadata(GetType(myCustomLabel), New FrameworkPropertyMetadata(GetType(myCustomLabel)))
End Sub
Public Property myEnglishText() As String
Get
Return MyBase.GetValue(myFrenchTextProperty)
End Get
Set(ByVal value As String)
MyBase.SetValue(myFrenchTextProperty, value)
End Set
End Property
Public Property myFrenchText() As String
Get
Return MyBase.GetValue(myFrenchTextProperty)
End Get
Set(ByVal value As String)
MyBase.SetValue(myFrenchTextProperty, value)
End Set
End Property
Private Sub TextChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
If DesignerProperties.GetIsInDesignMode(Me) = True Then
Me.Content = myEnglishText
Else
If myUser.Language = "E" Then
Me.Content = myEnglishText
Else
Me.Content = myFrenchText
End If
End If
End Sub
End Class
我的測試窗口電網XAML是簡單的:
<Grid>
<my:myCustomLabel myEnglishText="English Text" myFrenchText="English Text" Height="25" Width="100" Background="Aqua" Foreground="Black"/>
</Grid>
這似乎在開發環境中工作 - 不斷變化的英文和法文文本更改設計預覽和它運行時應用程序運行並打開測試窗口。但是,只有第一次 - 如果我打開測試窗口,我收到以下消息第二次:
「myEnglishText」屬性已經 通過「myCustomLabel」註冊。
我現在明白了,如果我將依賴屬性聲明更改爲共享,那麼這個問題就會消失 - 但這會導致許多其他問題,如需要共享回調函數 - 因此無法更新內容(需要用類實例化)。 我真正想要的是在英文和法文標籤更改時,設計時更新的內容屬性。
有沒有辦法解決這個問題?或者,也許是依賴屬性矯枉過正我需要什麼?
謝謝itowlson!依賴對象「d」整個時間都在注視着我,我從來沒有把兩個和兩個放在一起。 – Gatmando 2009-09-03 22:58:02