Binding a property on on a great-grandchild to an ancestor
I'm not even sure how to express this, so im very sorry if the title is confusing.. My XAML (simplified) looks like this:
<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>
where the L开发者_高级运维egendItem list is templated inside the legendcontrol.
The binding expression fails with a System.Windows.Data Error: 4. I've tried using elementname instead, with the same results. I'm guessing it has something to do with the LegendItems not actually being directly in the Visual tree but i have no idea (WPF rookie, i know). What am i doing wrong?
You have a typo in the AncestorType. You want to say FreqReserve.OverView. Also, you are going to have to reference the library's namespace as defined in your UserControl.
Something like this:
<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>
Note that you will have to put the correct namespace for the "local" declaration, but you should get that from IntelliSense if you aren't sure what it should be.
精彩评论