我在 Silverlight 中有一个带有 DependencyProperty 的自定义控件。如果控件本身更改了属性的值,那很好。但是,如果在服务器上更改了值,则控件必须重新加载其内容。
因此,在我的 PropertyChangedCallback 例程中,我希望能够检查是控件更改了值,还是在服务器上更改了(在这种情况下,需要重新加载内容)。
该控件是一个文本框,当它发生更改时,我正在执行 SetValue 来更改 DependencyProperty 值,但是 PropertyChangedCallback 例程不知道是调用它的控件还是服务器。
如何检查从何处调用 PropertyChangedCallback?
依赖属性:
Public Shared HtmlHolderProperty As DependencyProperty =
DependencyProperty.Register("HtmlHolder",
GetType(String),
GetType(HTMLEditor),
New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf OnHtmlHolderChanged)))
依赖属性更改处理程序:
Private Shared Sub OnHtmlHolderChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim hte As HTMLEditor = DirectCast(d, HTMLEditor)
Dim newhtml As String = If(e.NewValue Is Nothing, "", e.NewValue)
Dim oldhtml As String = If(e.OldValue Is Nothing, "", e.OldValue)
If newhtml <> m_HtmlHolder AndAlso ControlUpdate > ControlUpdateTracker Then
hte.htb.Load(Format.HTML, newhtml)
ControlUpdateTracker = ControlUpdateTracker + 1
End If
m_HtmlHolder = newhtml
End Sub
捆绑:
Private Sub CreateBindings(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim control As HTMLEditor = DirectCast(sender, HTMLEditor)
Dim context As IContentItem = DirectCast(control.DataContext, IContentItem)
Dim binding As Data.Binding
binding = New Data.Binding("Value") With {
.Source = context,
.Mode = Data.BindingMode.TwoWay
}
Me.SetBinding(HTMLEditor.HtmlHolderProperty, binding)
End Sub
控制 _ContentChanged:
Private Sub htb_ContentChanged(sender As Object, e As RichTextBoxEventArgs) Handles htb.ContentChanged
Dim htb As Liquid.RichTextBox = DirectCast(sender, Liquid.RichTextBox)
SetValue(HtmlHolderProperty,htp.HTML)
End Sub