【发布时间】:2014-08-14 12:12:21
【问题描述】:
我有一个非常直接的 MVVM Master\Detail 窗口 -
XAML sn-p:
<Grid Grid.Row="0" Grid.Column="0" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Content="ID:" Grid.Row="0" Grid.Column="0"/>
<Label Content="{Binding SelectedCustomer.CustId}" Grid.Row="0" Grid.Column="1"/>
<Label Content="Name:" Grid.Row="1" Grid.Column="0"/>
<TextBox Text="{Binding SelectedCustomer.Name}" Grid.Row="1" Grid.Column="1"/>
<Label Content="Address:" Grid.Row="2" Grid.Column="0"/>
<TextBox Text="{Binding SelectedCustomer.Address}" Grid.Row="2" Grid.Column="1"/>
<Label Content="City:" Grid.Row="3" Grid.Column="0"/>
<TextBox Text="{Binding SelectedCustomer.City}" Grid.Row="3" Grid.Column="1"/>
<Label Content="State:" Grid.Row="4" Grid.Column="0"/>
<TextBox Text="{Binding SelectedCustomer.State}" Grid.Row="4" Grid.Column="1"/>
<Label Content="ZIP:" Grid.Row="5" Grid.Column="0"/>
<TextBox Text="{Binding SelectedCustomer.ZIP}" Grid.Row="5" Grid.Column="1"/>
</Grid>
<Grid Grid.Row="1" Grid.Column="0">
<DataGrid ItemsSource="{Binding CustomerCollection}" SelectedItem="{Binding SelectedCustomer}"></DataGrid>
</Grid>
</Grid>
模型中没有什么花哨的东西:
public ObservableCollection<Customer> CustomerCollection { get; set; }
private Customer _selectedCustomer;
public Customer SelectedCustomer
{
get
{
return _selectedCustomer;
}
set
{
_selectedCustomer = value;
OnPropertyChanged("SelectedCustomer");
}
}
我可以选择一个主行,详细信息将适当填写。这部分效果很好。
当我编辑详细信息时,我的问题出现了,它在用户点击保存之前更改了主文件。如果我更改其中一个属性并且它失去焦点,则数据绑定会将主行设置为与新信息相同。我尝试了各种版本的 Mode=OneWay,但都没有帮助。
如何使 SelectedItem 的数据绑定只从数据网格中移出,而不是返回?当用户单击保存时,我将刷新数据网格,这不是问题。对我来说,记录不要在中途更改比过于频繁地刷新网格更重要。
【问题讨论】: