2017-03-07 45 views
0

您好,我在将数据从数据库绑定到应用程序中的视图时遇到了问题。我创建了一个数据库,并通过实体框架导入它,还有一个ViewModel,我创建了连接到我的数据库。我想从数据库中随机获取数据,并在我的标签中查看。这是我的代码:绑定来自数据库的数据以便在WPF MVVM中查看

视图模型

class TEST : INotifyPropertyChanged 
{ 

    public object RandomWords() 
    { 
     TABUEntities baza = new TABUEntities(); 
     baza.HASLA.ToList(); 
     var a = baza.HASLA.OrderBy(x => Guid.NewGuid()).Take(1); 
     return a; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 

模型 - 数据库

public partial class TABUEntities : DbContext 
{ 
    public TABUEntities() 
     : base("name=TABUEntities") 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     throw new UnintentionalCodeFirstException(); 
    } 

    public virtual DbSet<HASLA> HASLA { get; set; } 

} 

和我查看

<Window x:Class="Tabu.View.TEST" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    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:Tabu.View" 
    xmlns:vm="clr-namespace:Tabu.ViewModel" 
    mc:Ignorable="d" 
    Title="TEST" Height="600" Width="600"> 
<Window.DataContext> 
    <vm:TEST/> 
</Window.DataContext> 
<Grid> 
    <Label x:Name="label" Content="{Binding }" /> 
    <Label x:Name="label1" Content="{Binding }" /> 
    <Label x:Name="label2" Content="{Binding }" /> 
    <Label x:Name="label3" Content="{Binding }" /> 

</Grid> 

我不知道如何采取随机元素来自我的数据基地和视图进行绑定。在标签中,我想从我的数据库中添加列。任何人都可以给我一个提示吗?

回答

1

你可以使用一个ItemsControlDataGrid给显示在EF表中的项目,但您使用的视图模型类的公共财产暴露源集合:

public class TEST : INotifyPropertyChanged 
{ 
    public TEST() 
    { 
     SourceCollection = RandomWords(); 
    } 

    public System.Collections.IEnumerable SourceCollection { get; private set; } 

    public object RandomWords() 
    { 
     TABUEntities baza = new TABUEntities(); 
     var a = baza.HASLA.OrderBy(x => Guid.NewGuid()).Take(1).ToList(); 
     return a; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
} 

<Window x:Class="Tabu.View.TEST" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    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:Tabu.View" 
    xmlns:vm="clr-namespace:Tabu.ViewModel" 
    mc:Ignorable="d" 
    Title="TEST" Height="600" Width="600"> 
    <Window.DataContext> 
     <vm:TEST/> 
    </Window.DataContext> 
    <Grid> 
     <DataGrid ItemsSource="{Binding SourceCollection}" /> 
    </Grid> 
</Window> 

是否有可能在标签中显示数据库中的分隔列或不显示?

如果更改源集合属性IList的类型,你可以使用索引绑定到一个项目中它:

public IList SourceCollection { get; private set; } 

<Label x:Name="label1" Content="{Binding SourceCollection[0].Property}" /> 

“财产”是的属性的名称“ HASLA“实体,您要在Label中显示。

+0

是否有可能在标签中显示数据库中的分隔列或不显示? – Arkady

+0

您的'OnPropertyChanged'方法可以安全地更改为''handler' .Invoke(this,new PropertyChangedEventArgs(name));'在更高版本的C#中。请参阅https://codeblog.jonskeet.uk/2015/01/30/clean-event-handlers-invocation-with-c-6/ –

+0

@Arkady,请参阅我的编辑。 – mm8