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