C# WPF Master\Detail:编辑详细信息时,如何防止Master更新?
我有一个非常直接的MVVM主/详细窗口-C# WPF Master\Detail:编辑详细信息时,如何防止Master更新?,c#,wpf,mvvm,datagrid,C#,Wpf,Mvvm,Datagrid,我有一个非常直接的MVVM主/详细窗口- <Grid Grid.Row="0" Grid.Column="0" > <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinitio
<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>
XAML代码段:
<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>
模型中没有什么特别的东西:
<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");
}
}
public-observeCollection-CustomerCollection{get;set;}
私人客户(u选择的客户);
公共客户选择客户
{
得到
{
返回所选客户;
}
设置
{
_selectedCustomer=价值;
OnPropertyChanged(“SelectedCustomer”);
}
}
我可以选择一个主行,详细信息将适当填写。那部分效果很好
<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>
我的问题出现在编辑详细信息时,它会在用户点击“保存”之前更改主控形状。如果我更改了其中一个属性并且它失去了焦点,那么数据绑定会将主行设置为与新信息相同。我尝试过各种版本的Mode=one,但都没有用
<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>
如何使SelectedItem的数据绑定只从datagrid中传出,而不是返回?当用户单击Save时,我将刷新datagrid,这不是问题。对我来说,记录不在中途更改比经常刷新网格更重要。您可以使用
<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>
但在这种情况下,您必须运行所有详细表单中的控件,并手动更新源代码
<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>
更复杂的解决方案是,您可以将所选行加载到详细信息表单中,而不是从网格,而是从服务器再次加载。您必须在集合的属性上设置一次性绑定,而不是集合本身。
<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>
尝试禁用网格上的autogeneratecolumns,并使用一次性绑定自己定义列。这应该是你需要的行为
<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>
类似于此(未经测试):
<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>
但是,如果您确实想将详细信息与主信息分开,我建议您使用单独的selectedCustomer属性,并在编辑完成后将该属性复制到绑定到网格的集合中。一次性绑定很好,但是您必须手动更新网格,如果您想使用严格的MVVM,这需要一些代码。还请注意,当您编辑详细信息时,在ViewModel中选定的客户仍将在您的集合上更新,网格只是不会反映更改。因此,如果单击“保存”,最新信息仍将写入数据库。我将此标记为答案,因为它有一点帮助,并且提供了真正的答案:分离功能,以便重用。
<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>