WPF在不同级别绑定

时间:2012-12-04 11:32:19

标签: wpf data-binding binding user-controls wpf-controls

希望这对经验丰富的WPF用户来说是显而易见的。在绑定时,我必须考虑我绑定的目标的(嵌入)级别吗?

在下面的XAML中,我有MyTextBox,它可以接受我自己的额外validationRules列表,FileNameValidationRule有一个PathStr类型的ValidPathStr,它有一个InputPathStr字符串依赖属性。

为了测试我的理论,我尝试将MyTextBox的Text绑定到与MyTextBox的InputPathStr相同的绑定。 Text的绑定似乎有效(虽然不正确,但仍尝试解析绑定)但是对InputPathStr的绑定给出了“找不到框架导师”的错误。下面的跟踪显示了输出,第一个块是文本绑定,第二个块是失败的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

1 个答案:

答案 0 :(得分:2)

找到解决方案here。这是由于DataContext没有被遗传,显然。