2014-07-22 20 views
0

多天搜索被阻止,我放棄了並希望向您徵求意見。在DataTemplate中綁定封閉控件到依賴項對象的DP

我有以下的DataTemplate:

<TextBox x:Name="FilterValueTextBox" 
       Grid.Column="1" 
       BorderThickness="1" 
       BorderBrush="{Binding TextBoxBorderBrush}" 
       VerticalAlignment="Stretch" 
       IsEnabled="{Binding FilterTypeValid}" 
       Background="{Binding TextBoxBackgroundBrush}" 
       VerticalContentAlignment="Stretch" 
       Text="{Binding RawText}" 
       SnapsToDevicePixels="True"> 
    <controls:OnScreenKeyboardControl.KeyboardDescriptor> 
        <controls:KeyboardDescriptor TextBoxRef="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type TextBox}}}" /> 
    </controls:OnScreenKeyboardControl.KeyboardDescriptor> 
</TextBox> 

的KeyboardDescriptor是含有TextBox類型的依賴項屬性"TextBoxRef"一個DependencyObject

確切地說,這個機制在我的應用程序中無處不在,除了這種特殊情況,綁定應該發生在數據模板中。

我讀了幾十篇文章,指出那裏的所有東西都不是Visual或Logical Tree的一部分,因此ELementName和RelativeSource Binding應該失敗。

顯然我設法可靠地證實了這一點。 通過上面我得到了模板實例化的異常,「雙向綁定(在這種情況下是默認值)需要路徑或x路徑」... 沒有上述綁定嘗試,異常消失,但當然鏈接沒有建立。

我現在的問題是:我可以以某種方式將封閉文本框作爲參考傳遞到鍵盤描述符附加屬性的DP?

回答

0

自己完成。將上面的xaml更改爲:

<TextBox x:Name="FilterValueTextBox" 
       Grid.Column="1" 
       BorderThickness="1" 
       BorderBrush="{Binding TextBoxBorderBrush}" 
       VerticalAlignment="Stretch" 
       IsEnabled="{Binding FilterTypeValid}" 
       Background="{Binding TextBoxBackgroundBrush}" 
       VerticalContentAlignment="Stretch" 
       Text="{Binding RawText}" 
       SnapsToDevicePixels="True"> 
    <controls:OnScreenKeyboardControl.KeyboardDescriptor> 
        <controls:KeyboardDescriptor TextBoxRef="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TextBox}}, Path=.}" /> 
    </controls:OnScreenKeyboardControl.KeyboardDescriptor> 
</TextBox> 

儘管如此,非常感謝!希望這可以幫助別人有一天。

相關問題