希望这对于有经验的 WPF 用户来说是显而易见的。绑定时,我是否必须考虑要绑定的目标的(嵌入式)级别?
在下面的 XAML 中,我有 MyTextBox,它可以接受我自己的附加验证规则列表,FileNameValidationRule 有一个 PathStr 类型的 ValidPathStr,它有一个 InputPathStr 字符串依赖项属性。
为了测试我的理论,我尝试将 MyTextBox 的 Text 绑定设置为与 MyTextBox 的 InputPathStr 相同的绑定。Text 的绑定似乎有效(虽然不正确,但它仍然尝试解决绑定),但与 InputPathStr 的绑定给出了“未找到框架导师”错误。下面的跟踪显示了输出,第一个块是 Text 绑定,第二个块是失败的 InputPathStr 绑定。
我希望绑定要么都工作要么都失败,我应该如何更改 InputPathStr 的绑定以使其像 Text 一样工作?
谢谢。
<my2:MyTextBox x:Name="m_renameTextBox"
Text="{Binding Path=SourceBrowseDir.ItemFullPath, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" >
<my2:MyTextBox.AdditionalValidationRules>
<src:FileNameValidationRule>
<src:FileNameValidationRule.ValidPathStr>
<src:PathStr InputPathStr="{Binding Path=SourceBrowseDir.ItemFullPath, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
</src:FileNameValidationRule.ValidPathStr>
</src:FileNameValidationRule>
</my2:MyTextBox.AdditionalValidationRules>
</my2:MyTextBox >
输出是:
> Created BindingExpression (hash=3780752) for Binding (hash=63892678)
Path: 'SourceBrowseDir.ItemFullPath'
BindingExpression (hash=3780752): Attach to ScreenControls.HelperUserControls.MyTextBox.Text (hash=20793123)
BindingExpression (hash=3780752): Resolving source
BindingExpression (hash=3780752): Found data context element: MyTextBox (hash=20793123) (OK)
BindingExpression (hash=3780752): Activate with root item PresetsNavigatorViewModel (hash=34365244)
BindingExpression (hash=3780752): At level 0 using cached accessor for PresetsNavigatorViewModel.SourceBrowseDir: RuntimePropertyInfo(SourceBrowseDir)
BindingExpression (hash=3780752): Replace item at level 0 with PresetsNavigatorViewModel (hash=34365244), using accessor RuntimePropertyInfo(SourceBrowseDir)
BindingExpression (hash=3780752): GetValue at level 0 from PresetsNavigatorViewModel (hash=34365244) using RuntimePropertyInfo(SourceBrowseDir): <null>
BindingExpression (hash=3780752): Item at level 1 is null - no accessor
BindingExpression (hash=3780752): TransferValue - got raw value {DependencyProperty.UnsetValue}
BindingExpression (hash=3780752): TransferValue - using fallback/default value ''
BindingExpression (hash=3780752): TransferValue - using final value ''
>
> Created BindingExpression (hash=56631954) for Binding (hash=47211710)
Path: 'SourceBrowseDir.ItemFullPath'
BindingExpression (hash=56631954): Attach to ScreenControls.Helper.PathStr.InputPathStr (hash=40208759)
BindingExpression (hash=56631954): Use Framework mentor <null>
BindingExpression (hash=56631954): Resolving source
BindingExpression (hash=56631954): Framework mentor not found
BindingExpression (hash=56631954): Resolve source deferred