您好我的数据从数据库绑定到我的应用程序中的视图。我创建了一个数据库并通过Entity Framework导入它,还有一个ViewModel,我在其中创建了与我的数据库的连接。我想从数据库中获取随机数据并将其显示在我的标签视图中。这是我的代码:
视图模型
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>
我不知道如何从我的数据库中取一个随机元素并将其与视图绑定。在标签中,我想从我的数据库中添加列。谁能给我一个提示?
答案 0 :(得分:1)
如果您使用视图模型的公共属性公开源集合,则可以使用ItemsControl
(例如DataGrid
来显示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}" />
“Property”是您要在Label
中显示的“HASLA”实体的属性名称。