2015-11-02 26 views
1

我有一個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)); 


} 

和我在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> 

我在主窗口中設置斷點和它的DataContext和用戶屬性不爲null。但MainWindow.DataContext.Users和UsersControl.UsersCollection之間的綁定失敗。以下是從UsersControl.xaml.cs輸出窗口

System.Windows.Data Error: 40 : BindingExpression path error: 'Users' property not found on 'object' ''UsersControl' (Name='userControl')'. BindingExpression:Path=Users; DataItem='UsersControl' (Name='userControl'); target element is 'UsersControl' (Name='userControl'); target property is 'UsersCollection' (type 'ObservableCollection`1')

GetUsersCollection(DependencyObject的OBJ)總是返回null

回答

0

嘗試

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type c:UsersControl}}, Path=UsersCollection}" 

或者,你可以聰明一點;

<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} 

個人而言,我會建議使用這種方法,而不是第一的結合是更快,更不容易......「問題」 ......這也意味着你覆蓋用戶的模板控制是你真正想要的,而不僅僅是將一些XAML轉儲到其現有的模板中。

0

嘗試打電話給你的附加屬性像波紋管:

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