2013-03-14 50 views
2

我想創建我自己的GridViewColumn,並有一些綁定issus。爲什麼FindAncestor綁定在GridViewColumn中不起作用?

有人能向我解釋爲什麼下面的頭結合確實工作

<GridViewColumn x:Class="interneProzesse_UebersetzungstoolNS.TranslateGridViewColumn" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:conv="clr-namespace:interneProzesse_UebersetzungstoolNS.Converter" 
     xmlns:hk="clr-namespace:interneProzesse_UebersetzungstoolNS.Hilfsklassen" 
     xmlns:local="clr-namespace:interneProzesse_UebersetzungstoolNS" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"Header="{Binding RelativeSource={RelativeSource Self}, Path=Sprache, UpdateSourceTrigger=PropertyChanged}"> 
</GridViewColumn> 

而失敗嗎?

<GridViewColumn x:Class="interneProzesse_UebersetzungstoolNS.TranslateGridViewColumn" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:conv="clr-namespace:interneProzesse_UebersetzungstoolNS.Converter" 
     xmlns:hk="clr-namespace:interneProzesse_UebersetzungstoolNS.Hilfsklassen" 
     xmlns:local="clr-namespace:interneProzesse_UebersetzungstoolNS" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<GridViewColumn.Header> 
    <GridViewColumnHeader Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:TranslateGridViewColumn}}, Path=Sprache, UpdateSourceTrigger=PropertyChanged}"/> 
</GridViewColumn.Header> 
<GridViewColumn> 

Sprache是我TranslateGridViewColumn從GridViewColumn繼承的財產。

+0

你得到任何'BindingExpression'錯誤在你的調試輸出窗口? – DHN 2013-03-14 09:19:32

+0

爲什麼你不使用第一種形式,如果它工作? – nemesv 2013-03-14 09:21:13

+0

@DHN是的,它是「無法找到與參考綁定的源」RelativeSource FindAncestor,AncestorType ='interneProzesse_UebersetzungstoolNS.TranslateGridViewColumn',AncestorLevel ='1''。但是我想知道它爲什麼找不到,因爲'TranslateGridViewColumn'確實是一個祖先。 – 2013-03-14 09:24:49

回答

0

發佈問題幾分鐘後,我在這個thread找到了我的答案。 答案基本上來說,GridViewColumn不會被添加到可視化樹中,所以使用這個可視化樹的綁定(例如FindAncestor)不能工作。

所以我subsribed到我結合(如GridViewColumnHeader)元素的加載事件並沒有在代碼中隱藏的綁定:

BindingOperations.SetBinding(sender as GridViewColumnHeader, GridViewColumnHeader.ContentProperty, new Binding("Sprache") { Source = this, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Mode = BindingMode.OneWay });