使用Interface的ContentControl DataTemplate

时间:2012-07-11 03:17:16

标签: wpf binding interface datatemplate

我有一个显示用户列表的wpf应用程序。点击用户将显示该用户的详细信息 - 非常类似于WaF图书馆示例等。

我已经完成了一些关于绑定到接口的阅读,并且我可以成功地将ListView绑定到我的界面,但是使用与ContentControl相同的方法不起作用。

这就是我所拥有的:

域模型 - USER(EF实体)

partial class USER : IUser
    {
    }



public interface IUser
    {
        String USERNAME { get; set; }
        String FIRST_NAME { get; set; }
        String SURNAME {get;set;}
        String EMAIL { get; set; }
    }

表示层

    public class UserMainContentModel 
    {
          public ICollectionView Users { get; private set; }
          public IUser SelectedUser { get; private set; }
          //...
          private void SelectedUserChanged(object sender, EventArgs e)
          {
            SelectedUser = Users.CurrentItem as IUser;
             NotifyPropertyChanged("SelectedUser");
          }
    }

查看xaml

<UserControl x:Class="CQMS.Module.Users.Views.MainContent.UsersMainContentView"
             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:models="clr-namespace:CQMS.Module.Users.ViewModels"
             xmlns:interface="clr-namespace:CQMS.Framework.Model;assembly=CQMS.Framework"             
             mc:Ignorable="d" >
    <UserControl.Resources>
        <DataTemplate DataType="{x:Type interface:IUser}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Label AutomationProperties.AutomationId="UsernameLabel" Grid.Row="0" Grid.Column="0" Margin="0,0,0,10">Username</Label>
                <Label AutomationProperties.AutomationId="NameLabel" Grid.Row="0" Grid.Column="1">Name</Label>
                <Label AutomationProperties.AutomationId="EmailLabel" Grid.Row="0" Grid.Column="2">Email</Label>
                <TextBox AutomationProperties.AutomationId="UsernameTextBox" Grid.Row="1" Grid.Column="0" Text="{Binding interface:IUser.USERNAME}" />
                <TextBox AutomationProperties.AutomationId="NameTextBox" Grid.Row="1" Grid.Column="1" Text="{Binding interface:IUser.FIRST_NAME}" />
                <TextBox AutomationProperties.AutomationId="EmailTextBox" Grid.Row="1" Grid.Column="2" Text="{Binding interface:IUser.EMAIL}" />
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height=".4*"/>
            <RowDefinition Height=".6*"/>
        </Grid.RowDefinitions>
        <ListView Grid.Row="0" AutomationProperties.AutomationId="UserListView" ItemsSource="{Binding Users}"  SelectionMode="Single" Width="Auto" Height="Auto" >
            <ListView.ItemTemplate>
                <DataTemplate DataType="{x:Type interface:IUser}"/>
            </ListView.ItemTemplate>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Username" Width="80"
                         DisplayMemberBinding="{Binding Path=USERNAME}"/>
                    <GridViewColumn Header="First Name" Width="80"
                         DisplayMemberBinding="{Binding Path=FIRST_NAME}"/>
                    <GridViewColumn Header="Email" Width="80"
                         DisplayMemberBinding="{Binding Path=EMAIL}"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Border Grid.Row="1" CornerRadius="4,4,4,4" BorderBrush="#193441" Background="#FCFFF5" BorderThickness="2,2,2,2" Margin="5" Padding="5">
            <ContentControl Content="{Binding SelectedUser}"  Width="Auto" Height="Auto"/>
        </Border>
    </Grid>
</UserControl>

当我从列表中选择一个用户时,我想要的是所选用户的详细信息显示在内容控件中,但是,我得到的只是该控件中显示的具体类的名称。我不想将我的具体类暴露给我的视图,我真的不想将我的IUser对象包装在Model对象中......有没有办法做到这一点?我正确实现了这个吗?

1 个答案:

答案 0 :(得分:0)

您确定DataTemplates可以隐式应用于接口而不是类吗?无论哪种方式,在这种情况下,为模板提供密钥并在ContentControl.ContentTemplate中引用它都不会有害。

此外我从未见过像这样的绑定,路径不应该只是USERNAME而不是interface:IUser.USERNAME吗?