我有一个显示用户列表的 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");
}
}
查看xml
<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 对象中......有没有办法做到这一点?我是否正确实施了这一点?