2017-03-07 22 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