0

I'm using WPF to make a custom control, I need to retrieve a property that is defined in the user control code behind, so I used the RelativeSource, but 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=LeftColumnHeader; DataItem=null; target element is 'ExtDataGridComboBoxColumn' (HashCode=47761); target property is 'Header' (type 'Object')

My XAML code (the nested tree) is:

<UserControl x:Class="Administration.Views.UserRoleView"
         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:WPFCtrlDg="clr-namespace:WPFControls.DataGrids;assembly=WPFControls"
         xmlns:WPFCtrl="clr-namespace:WPFControls;assembly=WPFControls"
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<UserControl.Resources>

    <CollectionViewSource x:Key="AllItemsView" Source="{Binding Path='AllitemsList'}" />

</UserControl.Resources>

<Grid>


    <GroupBox Grid.Row="0" Grid.Column="0" Header="Assigned Elements">
        <WPFCtrlDg:SelfBindingDataGrid x:Name="_sbgAssigned" ScrollViewer.VerticalScrollBarVisibility="Auto"
                     ItemsSource="{Binding Path=Assigned}"
                     SelectedItem="{Binding Path=CurrentAssignedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <WPFCtrlDg:SelfBindingDataGrid.Columns>
                <WPFCtrlDg:ExtDataGridComboBoxColumn Header="{Binding Path=LeftColumnHeader,
                                                         RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}},
                                                          Mode=OneWay, 
                                                          UpdateSourceTrigger=PropertyChanged}" 
                                                          Width="*"/>

Inside the codebehid of the usercontrol I defined my property

  private string _leftColumnHeader = "TEST";
    public string LeftColumnHeader
    {
        get { return _leftColumnHeader; }
        set { _leftColumnHeader = value; }
    }
}

Any idea of how to retrieve my property in order to use it as head function of my satagrid column? thank you Andrea

andrea
  • 1,326
  • 7
  • 31
  • 60

2 Answers2

1

Use either

RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserRoleView}},
                Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"

Or add this to your UserControl:

<UserControl
    Name="myControl"
...

Then instead of using RelativeSource use binding like this:

Header={Binding Path=LeftColumnHeader, ElementName=myControl}

But actually I'm not quite sure that you still will be able to bind it that way you do it, as columns headers have some weird rules when it comes to binding. Check it:

stackoverflow.com

stackoverflow.com

Community
  • 1
  • 1
Tafari
  • 2,639
  • 4
  • 20
  • 28
  • @andrea unfortunately, I think you could find the solution in the first link, bottom part of the question there contains some answer. – Tafari Oct 25 '13 at 12:17
  • I used something like that in some parts of my code, now is a bit more complicated. I use MVVM, and if I set this property in the Viewmodel related to my user control it works, in this case the property in the codebehind (xaml.cs) because for some reason i would prefer to leave them there. So doing in that way will search in the viewmodel and not in the codebehind – andrea Oct 25 '13 at 12:34
  • @andrea doing what? As I do not really get your comment. All I understand is that you managed to bind the header but only when you set the property in the Viewmodel right? – Tafari Oct 25 '13 at 13:03
0

You need to use the name/type of your class, not the UserControl class that you are extending. Try this:

<WPFCtrlDg:ExtDataGridComboBoxColumn Header="{Binding Path=LeftColumnHeader, 
    RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type 
    UserRowView}}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
Sheridan
  • 68,826
  • 24
  • 143
  • 183