2010-10-13 55 views
1

我什至不知道如何表達這一點,所以即時通訊非常抱歉,如果標題是混亂。我的XAML(簡體)看起來是這樣的:綁定在一個曾孫上的屬性,以祖先

<UserControl x:Class="PBA.Application.Client.UserControls.UserControls.FreqReserve.OverView" xmlns:FreqReserve="clr-namespace:PBA.Application.Client.UserControls.UserControls.FreqReserve"> 
... 
    <DockPanel> 
     <UserControls:LegendControl> 
      <UserControls:LegendControl.Items> 
       <UserControls:LegendItem Visibility="{Binding Path=IsDirtyVisible, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type FreqReserve:OverView}}, Converter={StaticResource btvc}}" Identifier="Pink" Text="Ikke sendt"></UserControls:LegendItem> 
       <UserControls:LegendItem Identifier="Yellow" Text="Sendt"></UserControls:LegendItem> 
       <UserControls:LegendItem Identifier="LightGreen" Text="Accepteret"></UserControls:LegendItem> 
       <UserControls:LegendItem Identifier="White" Text="Ikke accepteret"></UserControls:LegendItem> 
      </UserControls:LegendControl.Items> 
     </UserControls:LegendControl> 
    </DockPanel> 
</UserControl> 

其中LegendItem列表模板化在legendcontrol中。

綁定表達式失敗,出現System.Windows.Data錯誤:4.我嘗試過使用elementname,而使用相同的結果。我猜這與LegendItems有些關係,並不直接在Visual樹中,但我不知道(WPF菜鳥,我知道)。我究竟做錯了什麼?

回答

1

您在AncestorType中有錯字。你想說FreqReserve.OverView。此外,您將不得不引用UserControl中定義的庫的名稱空間。

事情是這樣的:

<UserControl x:Class="PBA.Application.Client.UserControls.UserControls.FreqReserve.OverView" 
      ... 
      xmlns:local="clr-namespace:PBA.Application.Client.UserControls.UserControls"> 

    ... 
     <DockPanel> 
     <UserControls:LegendControl> 
      <UserControls:LegendControl.Items> 
       <UserControls:LegendItem IsVisible="{Binding Path=IsDirtyVisible, 
        RelativeSource={RelativeSource Mode=FindAncestor, 
            AncestorType={x:Type local:FreqReserve.OverView}}}" 
        Identifier="Pink" 
        Text="Ikke sendt"></UserControls:LegendItem> 

       ....   

     </UserControls:LegendControl> 
    </DockPanel> 
</UserControl> 

請注意,您必須把正確的命名空間爲「本地」的聲明,但你應該從智能感知,如果你不知道它應該是什麼。

+0

對不起。在去除絨毛的時候稍微過了一會兒,並且用手寫了一段代碼,因爲它不再是實際的代碼。代碼段已更新以更好地反映實際代碼。 – hhravn 2010-10-13 13:51:30

+0

讓我們從簡單的開始,並努力解決問題。 IsDirtyVisible是OverView控件的屬性嗎? – 2010-10-13 14:14:45

+0

是的,它是一個包裝的依賴屬性。 – hhravn 2010-10-13 14:47:51