1
votes

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");
    }
}
1
Why u have set IsSynchronizedWithCurrentItem="True" and how you are getting values in your textboxes ?AnjumSKhan

1 Answers

0
votes

I would create a binding for DataGrid's SelectedItem.

Then in your SelectedItem's properties' Setter, create a clone of it to a new property.

Bind your information panel's elements to the cloned object, so all their changes do not affect the original row.

On the save command, copy the properties changed on the cloned object back to the selected object.