C# 在我的用户控件中附加的属性绑定失败

C# 在我的用户控件中附加的属性绑定失败,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我有一个UsersControl.xaml <UserControl x:Name="userControl" x:Class="Lloyd.Shared.Controls.UsersControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我有一个UsersControl.xaml

<UserControl x:Name="userControl" x:Class="Lloyd.Shared.Controls.UsersControl"
         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:Interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
         xmlns:Behaviors="clr-namespace:Lloyd.Shared.Controls.Behaviors"
         xmlns:local="clr-namespace:Lloyd.Shared.Controls"
         mc:Ignorable="d" DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}"
         d:DesignHeight="300" d:DesignWidth="300">

<Interactivity:Interaction.Behaviors>
    <Behaviors:ImportUsersOnLoadedBehavior />
</Interactivity:Interaction.Behaviors>
<DockPanel LastChildFill="True">
    <Label Content="账号" DockPanel.Dock="Top" FontSize="18.667" />

    <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Right">
        <Button x:Name="AddAccountButton" Height="26" Width="80" Content="添加" Margin="5" Command="{Binding AddUserCommand}" />
        <Button x:Name="ImportAccountsButton" Height="26" Width="80" Content="导入" Margin="5" Command="{Binding ImportUsersCommand}"/>
        <Button x:Name="DeleteAccountButton" Height="26" Width="80" Content="删除" Margin="5" Command="{Binding RemoveUsersCommand}"/>
    </StackPanel>

    <ListView x:Name="accountListbox" Margin="5" BorderThickness="1" BorderBrush="{DynamicResource AccentColorBrush}" ItemsSource="{Binding UsersCollection}"/>
</DockPanel>

代码隐藏

public partial class UsersControl : UserControl
{
    public static ObservableCollection<User> GetUsersCollection(DependencyObject obj)
    {
        return (ObservableCollection<User>)obj.GetValue(UsersCollectionProperty);
    }

    public static void SetUsersCollection(DependencyObject obj, ObservableCollection<User> value)
    {
        obj.SetValue(UsersCollectionProperty, value);
    }

    // Using a DependencyProperty as the backing store for UsersCollection.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UsersCollectionProperty =
        DependencyProperty.RegisterAttached("UsersCollection", typeof(ObservableCollection<User>), typeof(UsersControl), new FrameworkPropertyMetadata(null,FrameworkPropertyMetadataOptions.AffectsRender));


}
公共部分类UsersControl:UserControl
{
公共静态ObservableCollection GetUsersCollection(DependencyObject obj)
{
return(observeCollection)obj.GetValue(UsersCollectionProperty);
}
公共静态void SetUsersCollection(DependencyObject对象,ObservableCollection值)
{
SetValue(UsersCollectionProperty,value);
}
//使用DependencyProperty作为UsersCollection的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty UsersCollectionProperty=
DependencyProperty.RegisterAttached(“UsersCollection”、typeof(ObservableCollection)、typeof(UsersControl)、new FrameworkPropertyMetadata(null,FrameworkPropertyMetadata Options.AffectsRender));
}
我在MainWindow.xaml中使用这个UsersControl,并将UsersCollection绑定到主窗口的DataContext

<Controls:MetroWindow x:Class="ZggwwDocuments.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:c="clr-namespace:Lloyd.Shared.Controls;assembly=Lloyd.Shared.Controls"
    xmlns:vm="clr-namespace:ZggwwDocuments.ViewModels"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ZggwwDocuments"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="697.514">
<Controls:MetroWindow.DataContext>
    <vm:MainWindowViewModel/>
</Controls:MetroWindow.DataContext>
<Grid Margin="5">
    <c:UsersControl UsersCollection="{Binding Users}"  />
</Grid>

我在MainWindow上设置了断点,它的DataContext和Users属性不为null。但是MainWindow.DataContext.Users和UsersControl.UsersCollection之间的绑定失败。以下是“输出”窗口中的内容

System.Windows.Data错误:40:BindingExpression路径错误:“在“对象”“UsersControl'(Name='userControl')”上找不到Users属性。”。BindingExpression:Path=Users;DataItem='UsersControl'(Name='userControl');目标元素是“UsersControl”(Name='userControl');目标属性为“UsersCollection”(类型为“ObservableCollection`1”)


UsersControl.xaml.cs中的GetUsersCollection(DependencyObject obj)始终返回null

尝试调用附加属性,如下面所示:

<c:UsersControl UsersControl.UsersCollection="{Binding Users}"  />

试试看

或者,你可以聪明一点

<DockPanel LastChildFill="True">
    <Label Content="账号" DockPanel.Dock="Top" FontSize="18.667" />

    <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Right">
        <Button x:Name="AddAccountButton" Height="26" Width="80" Content="添加" Margin="5" Command="{Binding AddUserCommand}" />
        <Button x:Name="ImportAccountsButton" Height="26" Width="80" Content="导入" Margin="5" Command="{Binding ImportUsersCommand}"/>
        <Button x:Name="DeleteAccountButton" Height="26" Width="80" Content="删除" Margin="5" Command="{Binding RemoveUsersCommand}"/>
    </StackPanel>

    <ListView x:Name="accountListbox" Margin="5" BorderThickness="1" BorderBrush="{DynamicResource AccentColorBrush}" ItemsSource="{Binding UsersCollection}"/>
</DockPanel>
就个人而言,我建议使用这种方法,而不是第一种方法,因为绑定速度更快,更不容易问题“。。。这还意味着您将覆盖用户控件的模板,使其成为您真正想要的模板,而不是在现有模板中转储一些XAML

<DockPanel LastChildFill="True">
    <Label Content="账号" DockPanel.Dock="Top" FontSize="18.667" />

    <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Right">
        <Button x:Name="AddAccountButton" Height="26" Width="80" Content="添加" Margin="5" Command="{Binding AddUserCommand}" />
        <Button x:Name="ImportAccountsButton" Height="26" Width="80" Content="导入" Margin="5" Command="{Binding ImportUsersCommand}"/>
        <Button x:Name="DeleteAccountButton" Height="26" Width="80" Content="删除" Margin="5" Command="{Binding RemoveUsersCommand}"/>
    </StackPanel>

    <ListView x:Name="accountListbox" Margin="5" BorderThickness="1" BorderBrush="{DynamicResource AccentColorBrush}" ItemsSource="{Binding UsersCollection}"/>
</DockPanel>
<UserControl.Template>
   <ControlTemplate TargetType="{x:Type UserControl}">
      (paste it here)
   </ControlTemplate>
</UserControl.Template>
{Binding RelativeSource={RelativeSource TemplatedParent}, Path=UsersCollection}