none
Unable to bind to a Dependency Property in a UserControl from within another UserControl RRS feed

  • Question

  • Sorry if the title is a little confusing.

    I have a UserControl with a boolean Dependency Property called "ShowLegend." I can bind to this property just fine by doing the following:

    <CheckBox IsChecked="{Binding Path=ShowLegend, RelativeSource={RelativeSource AncestorType=UserControl}}" />

    And it works as expected.

    However, I also have a 3rd party control that I'm trying to bind ShowLegend to but it's not working. Here's a quick example of that control:

    <sci:SciChartSurface>
        <sci:ModifierGroup>
            <sci:LegendModifier ShowLegend="{Binding Path=ShowLegend, RelativeSource={RelativeSource AncestorType=UserControl}}" />
        </sci:ModifierGroup>
    </sci:SciChartSurface>

    The above gives me the following error:

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=ShowLegend; DataItem=null; target element is 'LegendModifier' (Name=''); target property is 'ShowLegend' (type 'Boolean')

    And I've tried other variations of bindings but I keep getting errors. For example:

    This binding, which references the control its in:

    {Binding Path=ShowLegend, RelativeSource={RelativeSource AncestorType=local:DataChart}}

    Gives me this error:

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MyProgram.UserControls.DataChart', AncestorLevel='1''. BindingExpression:Path=ShowLegend; DataItem=null; target element is 'LegendModifier' (Name=''); target property is 'ShowLegend' (type 'Boolean')

    If I try any number of these:

    {Binding Path=ShowLegend, RelativeSource={RelativeSource AncestorLevel=1, AncestorType=UserControl}}

    I get this error:

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=ShowLegend; DataItem=null; target element is 'LegendModifier' (Name=''); target property is 'ShowLegend' (type 'Boolean')

    I'm just not sure of the best way to have it bind to the same Dependency Property as the CheckBox.

    Thanks!

    Thursday, April 25, 2019 3:18 PM

All replies

  • >>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=ShowLegend; DataItem=null; target element is 'LegendModifier' (Name=''); target property is 'ShowLegend' (type 'Boolean')

    Hi T Gregory,

    According to your description, I guess that the LegendModifier is not part of the visual or logical tree, and therefore has no binding ancestor (or any ancestor), so the RelativeSource doesn't work.
    Instead you have to give the binding the source explicitly.

    Here is the same article that you can take a look:

    https://stackoverflow.com/questions/15494226/cannot-find-source-for-binding-with-reference-relativesource-findancestor

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 26, 2019 5:32 AM
    Moderator
  • It's weird that when I use the <sci:SciChartSurface> control outside of a UserControl (like in MainWindow.xaml) I can bind to the property in a ViewModel just fine. It's when I place the control in a UserControl I'm having difficulty.
    Friday, April 26, 2019 2:11 PM
  • Hi,
    you can set the DataContext of <sci:SciChartSurface> to your UserControl like this:

    in XAML
    
    <sci:SciChartSurface x:Name="uc"/>
    
    in CodeBehind of your UserControl:
    
      Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        uc.DataContext = Me
      End Sub
    
    or in C#
    
        public YourUserControl()
        {
          InitializeComponent();
          uc.DataContext = this;
        }
    
    



    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, April 27, 2019 2:45 AM