Posted 16 years ago
by Billy Jacobs
Version: 4.5.0485
Platform: .NET 3.5
Environment: Windows Vista (32-bit)

RelativeSource with FindAncestor does not work correctly for controls nested inside of a Popup button control:
If I do not nest my control inside of the Popup button control I get no error and it works correctly. When placing my control inside of a popup button control I get the following error:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='StevenDale.PediNotes.Client.TestWindow', AncestorLevel='1''. BindingExpression:Path=DataContext.LabelText; DataItem=null; target element is 'Label' (Name='label1'); target property is 'Content' (type 'Object')
Here is the XAML for a small test case I wrote to reproduce the problem. I have a label that is not nested inside of the popup button and another label that is nested inside of a popup button. The one outside works. The one inside does not.Here is the code behind.
Thanks In Advance,
Billy Jacobs
If I do not nest my control inside of the Popup button control I get no error and it works correctly. When placing my control inside of a popup button control I get the following error:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='StevenDale.PediNotes.Client.TestWindow', AncestorLevel='1''. BindingExpression:Path=DataContext.LabelText; DataItem=null; target element is 'Label' (Name='label1'); target property is 'Content' (type 'Object')
Here is the XAML for a small test case I wrote to reproduce the problem. I have a label that is not nested inside of the popup button and another label that is nested inside of a popup button. The one outside works. The one inside does not.
<Window x:Class="StevenDale.PediNotes.Client.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:control="clr-namespace:StevenDale.PediNotes.Client.Controls"
xmlns:uc="clr-namespace:StevenDale.PediNotes.Client.UserControls"
xmlns:local="clr-namespace:StevenDale.PediNotes.Client"
Title="TestWindow" xmlns:my="clr-namespace:ActiproSoftware.Windows.Controls;assembly=ActiproSoftware.Shared.Wpf30">
<StackPanel>
<Label Name="label2" Content="{Binding Path=DataContext.LabelText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:TestWindow}}}" DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:TestWindow}}}" />
<my:PopupButton Name="popupButton1">
<my:PopupButton.PopupContent>
<Label Name="label1" Content="{Binding Path=DataContext.LabelText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:TestWindow}}}" />
</my:PopupButton.PopupContent>
</my:PopupButton>
</StackPanel>
</Window>
public partial class TestWindow : Window
{
private class TestObject2
{
public string LabelText { get; set; }
}
public TestWindow()
{
this.DataContext = new TestObject2 { LabelText = "Test Label Text" };
InitializeComponent();
}
}
Billy Jacobs