0

我正在使用 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 列的头功能的任何想法?谢谢安德里亚

4

2 回答 2

1

使用任一

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

或将其添加到您的 UserControl:

<UserControl
    Name="myControl"
...

然后不要使用RelativeSource,而是使用这样的绑定:

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

但实际上我不太确定您是否仍然能够以这种方式绑定它,因为列标题在绑定时有一些奇怪的规则。核实:

stackoverflow.com

stackoverflow.com

于 2013-10-25T11:42:15.607 回答
0

您需要使用类的名称/类型,而不是您要扩展的UserControl。尝试这个:

<WPFCtrlDg:ExtDataGridComboBoxColumn Header="{Binding Path=LeftColumnHeader, 
    RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type 
    UserRowView}}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
于 2013-10-25T11:15:16.743 回答