我正在使用 WPF 制作自定义控件,我需要检索在后面的用户控件代码中定义的属性,所以我使用了 RelativeSource,但是我收到了这个错误
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')
我的 XAML 代码(嵌套树)是:
<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="*"/>
在用户控件的代码隐藏中,我定义了我的属性
private string _leftColumnHeader = "TEST";
public string LeftColumnHeader
{
get { return _leftColumnHeader; }
set { _leftColumnHeader = value; }
}
}
关于如何检索我的属性以将其用作我的 satagrid 列的头功能的任何想法?谢谢安德里亚