In my MainWindow , I have DataGrid and StackPanel with set of TextBox Control(one for each column) and save button.
When user selects a row in DataGrid then current row's details will be displayed in text box. User can edit the information in text box and save (During save, the source will be updated. used UpdateSourceTrigger=Explicit)
The problem is ,the source is updated when selected another row in DataGrid. But I want to update only on Save button click.
Sample Code:
MainWindow.xaml
<Window x:Class="PrismTestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PrismTestProject"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Model x:Key="model"/>
</Window.Resources>
<DockPanel DataContext="{StaticResource model}">
<DataGrid ItemsSource="{Binding Table, UpdateSourceTrigger=Explicit}" DockPanel.Dock="Left" IsSynchronizedWithCurrentItem="True"/>
<StackPanel DockPanel.Dock="Right">
<WrapPanel>
<Label Content="Name" Width="100"/> <TextBox Name="tbxName" Text="{Binding Table/Name, UpdateSourceTrigger=Explicit}" Width="100"/>
</WrapPanel>
<WrapPanel>
<Label Content="Age" Width="100"/>
<TextBox Name="tbxAge" Text="{Binding Table/Age,UpdateSourceTrigger=Explicit}" Width="100"/>
</WrapPanel>
<WrapPanel>
<Label Content="Qualification" Width="100"/>
<TextBox Name="tbxQualification" Text="{Binding Table/Qualification,UpdateSourceTrigger=Explicit}" Width="100"/>
</WrapPanel>
<Button Click="SaveButton_Click" Content="Save"/>
</StackPanel>
</DockPanel>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
tbxName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
tbxAge.GetBindingExpression(TextBox.TextProperty).UpdateSource();
tbxQualification.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
}
Model.cs
public class Model
{
public DataTable Table { get; set; }
public Model()
{
this.Table = new DataTable("Sample");
this.Table.Columns.Add("Name");
this.Table.Columns.Add("Age");
this.Table.Columns.Add("Qualification");
this.Table.Rows.Add("User 1", "55", "PG");
this.Table.Rows.Add("User 1", "40", "UG");
this.Table.Rows.Add("User 1", "35", "UG");
this.Table.Rows.Add("User 1", "60", "PG");
}
}
IsSynchronizedWithCurrentItem="True"
and how you are getting values in your textboxes ? – AnjumSKhan