【发布时间】:2013-12-09 01:52:02
【问题描述】:
我正在尝试将数据保存到数据库。
假设我有一个名为 Customers 的表,其中包含三个字段:
Id
FirstName
LastName
我使用 ADO.Net 实体数据模型创建了我的模型。
这是我的 ViewModel 代码
public class myViewModel : INotifyPropertyChanged
{
private string _firstName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
OnPropertyChanged("FirstName");
}
}
private string _lastName;
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
OnPropertyChanged("LastName");
}
}
protected virtual void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(PropertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
这是我的 MainWindow.xaml 文件:
<Window x:Class="Lab_Lite.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Lab_Lite.ViewModels"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized">
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="FirstName" />
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="LastName" />
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />
<Button Grid.Row="2" Grid.Column="1" Content="Save" />
</Grid>
</Window>
这里有两个问题:
1. How my ViewModel knows that FirstName property declared in ViewModel is
referenced to FirstName Column in my database?
2. How to save changes to database when UpdateSourceTrigger is set to Explicit?
我想我已经在某种程度上使用命令找到了第二个问题的答案。但是我不知道它是否正确,因为我不知道我的第一个问题的答案。
更新:
假设我有两个这样的表:
客户:
CustomerID
Name
GenderID //Foreign Key
性别:
GenderID
Value
现在SaveCustomerChanges 方法中CurrentCustomer.Gender 的值应该是多少?
【问题讨论】:
标签: c# wpf silverlight xaml mvvm