WPF自定义StackPanel附加属性OneWayToSource绑定失效求助
问题分析与解决方案
这个问题的核心坑点在于你用SetValue直接设置附加属性时,会覆盖掉该属性上已有的绑定!虽然Snoop能看到附加属性的值变成了"Filled",但此时原来的OneWayToSource绑定已经被清除了,自然无法把值同步到视图模型的Caption属性。
WPF里的SetValue方法会直接替换依赖属性的本地值,同时移除该属性上的所有绑定关联。而你需要的是在保留绑定的前提下更新属性值,让绑定系统能感知到变化并同步到源——这时候应该用SetCurrentValue方法。
修改后的StackPanelEx代码
Public Class StackPanelEx Inherits StackPanel Public Shared TestProperty As DependencyProperty = DependencyProperty.RegisterAttached("Test", GetType(String), GetType(StackPanelEx), New FrameworkPropertyMetadata("Empty")) Public Shared Function GetTest(ByVal d As DependencyObject) As String Return d.GetValue(StackPanelEx.TestProperty) End Function Public Shared Sub SetTest(ByVal d As DependencyObject, ByVal value As String) d.SetValue(StackPanelEx.TestProperty, value) End Sub Protected Overrides Function ArrangeOverride(finalSize As Size) As Size Dim result As Size Dim index As Integer result = MyBase.ArrangeOverride(finalSize) For Each child As UIElement In MyBase.InternalChildren index += 1 If (index Mod 2) = 0 Then ' 用SetCurrentValue替代SetValue,保留绑定同时更新值 child.SetCurrentValue(StackPanelEx.TestProperty, "Filled") End If Next child Return result End Function End Class
为什么这样能解决问题?
SetCurrentValue的作用是设置依赖属性的有效值,但不会改变属性的绑定或本地值优先级。也就是说:
- 原来的
OneWayToSource绑定会被保留 - 当属性值通过
SetCurrentValue更新后,绑定系统会检测到这个变化,自动把新值同步到视图模型的Caption属性
这样你就能在视图模型中正确获取到附加属性的更新值了。
内容的提问来源于stack exchange,提问作者Nostromo
相关产品推荐
相关产品推荐

