我正在玩WPF和LINQ,并坚持下面的一点,不知道如何显示单个记录,我发现的所有示例都显示如何显示列表,但不是单个记录。<登记/> 我准备了一个用户控件,它应该一次显示一条记录,在我的例子中是一个Customer,显示它的'Id,Given Name和Surname,这里是:
<UserControl x:Class="SportsClubsManagement.CustomersManagement.CustomerData"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500">
<Grid DataContext="Binding Customer">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Id" Height="30" Width="70" />
<TextBox Grid.Row="0" Grid.Column="1" Height="20" Width="70" Name="Id" Text="{Binding Path=Id}" />
<Label Grid.Row="1" Grid.Column="0" Content="Given Name" Height="30" Width="70" />
<TextBox Grid.Row="1" Grid.Column="1" Height="20" Width="70" Name="GivenName" Text="{Binding Path=GivenName}" />
<Label Grid.Row="1" Grid.Column="2" Content="Surname" Height="30" Width="70" />
<TextBox Grid.Row="1" Grid.Column="3" Height="20" Width="70" Name="Surname" Text="{Binding Path=Surname}" />
<Button Grid.Row="2" Grid.Column="0" Content="New record" />
<Button Grid.Row="2" Grid.Column="3" Content="Next record" />
<Button Grid.Row="2" Grid.Column="2" Content="Prev record" />
</Grid>
</UserControl>
我还创建了一个包含多个记录的表和一个代表客户的类,该客户也实现了INotifyPropertyChanged。
[Table(Name = "Customers")]
public class Customer : INotifyPropertyChanged
{
#region Fields
private String id;
private String givenName;
private String surname;
#endregion
#region Properties
[Column(IsPrimaryKey=true, Storage="id", Name="Id")]
public String Id
{
get { return this.id; }
}
[Column(Storage="givenName", Name="GivenName")]
public String GivenName
{
get { return this.givenName; }
set {
if (this.givenName != value)
{
this.givenName = value;
this.OnPropertyChanged("GivenName");
}
}
}
[Column(Storage="surname", Name="Surname")]
public String Surname
{
get { return this.surname; }
set {
if (this.surname != value)
{
this.surname = value;
this.OnPropertyChanged("Surname");
}
}
}
#endregion
#region INotifyPropertyChanged Members"
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(String name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
现在在用户控件背后的代码中,我有一个非常简单的代码连接到db并获取记录:
public partial class CustomerData : UserControl
{
private int curCustSeqNum;
private Data.Customer customer;
public Data.Customer Customer
{
get
{
return customer;
}
set
{
this.customer = value;
}
}
private IQueryable<Data.Customer> custQuery;
public CustomerData()
{
InitializeComponent();
DataContext db = new DataContext(@"server=WIN-EL78137MUMS\SQLEXPRESS;database=PlayGround;integrated security=SSPI");
// Get a typed table to run queries.
Table<Data.Customer> Customers = db.GetTable<Data.Customer>();
//db.Log = Console.Out;
custQuery =
from cust in Customers
orderby cust.Id
select cust;
curCustSeqNum = 0;
Customer = custQuery.Skip(curCustSeqNum).Take(1).First();
}
}
但是如何将文本框绑定到当前选定的客户?在定义资源等之后,您可以看到我的最后一次猜测。但每次我都会错过一些东西。我想知道如何在XAML和后面的代码中实现这一点。基本上之后,我可以使用curCustSeqNum跳到以下记录。
答案 0 :(得分:1)
CustomerData
没有实现INotifyPropertyChanged
所以当你在构造函数中设置Customer时,xaml没有想法它已经改变了。
示例:
public partial class CustomerData : UserControl, INotifyPropertyChanged
{
private int curCustSeqNum;
private Data.Customer customer;
private IQueryable<Data.Customer> custQuery;
public Data.Customer Customer
{
get { return customer; }
set { this.customer = value; OnPropertyChanged("Customer"); }
}
public CustomerData()
{
InitializeComponent();
// set the datacontext
DataContext = this;
................
Customer = custQuery.Skip(curCustSeqNum).Take(1).First();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(String name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
其他人也提到过,你对Grid上的DataContext绑定无效
<Grid DataContext="Binding Customer">
应该是
<Grid DataContext="{Binding Customer}">
答案 1 :(得分:0)
通常,您在ViewModel中创建一个“CurrentItem”对象,您可以在其中初始化SelectionCommand。在您的情况下,您将在SelectionCommand上绑定您的“客户”,在该命令中您运行Customer.Get(id)命令以获取当前客户。然后,您可以将控件绑定到每个控件的字段的“CurrentCustomer”对象。
答案 2 :(得分:0)
尝试将Datacontext设置为
DataContext="{Binding Path=Customer}"
我意识到道路是暗示的,但是我自己遇到了这个问题,这为我解决了这个问题。