我的 XAML:
<Window x:Class="Newsletter.UI.MessagesWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MessagesWindow" Name="messagesWindow" Height="576" Width="1024" WindowStartupLocation="CenterScreen" Closed="MessagesWindowClosed">
<Window.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Width" Value="149"/>
<Setter Property="Margin" Value="10"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Name="saveButton" Grid.Row="0" Content="Save" Margin="10,10,10,5"/>
<TextBox Name="searchTextBox" Grid.Row="1" Margin="10,5,10,10"/>
</Grid>
<Button Name="recipientsButton" Content="RECIPIENTS" Click="RecipientsButtonClick" />
<Button Name="createButton" Content="CREATE" Click="CreateButtonClick" />
<Button Name="removeButton" Content="REMOVE" />
<Button Name="editButton" Content="EDIT" />
<Button Name="resendButton" Content="RESEND"/>
</StackPanel>
<DataGrid Name="gridMessages" Grid.Row="1"/>
</Grid>
</Window>
我的 .cs 代码:
private void messagesWindow_Loaded(object sender, RoutedEventArgs e)
{
using (NewsletterEntities context = new NewsletterEntities())
{
var query = from m in context.Messages select m;
var result = query.ToList();
gridMessages.DataContext = result;
}
}
Message
由 EF 生成的类:
[EdmEntityTypeAttribute(NamespaceName="NewsletterModel", Name="Message")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Message : EntityObject
{
#region Factory Method
/// <summary>
/// Create a new Message object.
/// </summary>
/// <param name="messageID">Initial value of the MessageID property.</param>
/// <param name="subject">Initial value of the Subject property.</param>
/// <param name="content">Initial value of the Content property.</param>
/// <param name="hasAttachments">Initial value of the HasAttachments property.</param>
/// <param name="senderID">Initial value of the SenderID property.</param>
/// <param name="date">Initial value of the Date property.</param>
public static Message CreateMessage(global::System.Int32 messageID, global::System.String subject, global::System.String content, global::System.Boolean hasAttachments, global::System.Int32 senderID, global::System.DateTime date)
{
Message message = new Message();
message.MessageID = messageID;
message.Subject = subject;
message.Content = content;
message.HasAttachments = hasAttachments;
message.SenderID = senderID;
message.Date = date;
return message;
}
#endregion
#region Primitive Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 MessageID
{
get
{
return _MessageID;
}
set
{
if (_MessageID != value)
{
OnMessageIDChanging(value);
ReportPropertyChanging("MessageID");
_MessageID = StructuralObject.SetValidValue(value);
ReportPropertyChanged("MessageID");
OnMessageIDChanged();
}
}
}
private global::System.Int32 _MessageID;
partial void OnMessageIDChanging(global::System.Int32 value);
partial void OnMessageIDChanged()
不幸的是,什么也没发生,DataGrid 是空的。我是 WPF 和 EF 中数据绑定的新手。我希望这个问题很简单,但我不知道如何解决它。