Bilateral binding in WPF via LINQ to SQL for ListItem ListBox property using DataTemplate

I am new to C # and WPF. Despite the extensive documentation, tutorials, and previous questions I read, I cannot find my script.

What i have

I have a WPF application in C # with a single window containing a ListBox. A ListBox defines a DataTemplate to display its elements with a TextBox for each ListItem.

I have a Microsoft SQL Express Server on the same computer that contains the database with the name of Testthe submission of the name Client, which provides columns id INT PRIMARY KEY IDENTITYand name VARCHAR(50) NOT NULLother table and stored procedure name Client_update(@id, @name)that sets a column nameclient, idwhich @idthrough a simple UPDATE statement in the same table .

What I'm trying to achieve

I am trying to ensure that the ListBox is populated by loading the window and that changes to the data (which are contained in the text fields) are saved in the database (via a stored procedure) by switching the focus to another GUI component.

What I've done

LINQ to SQL Classes Visual Studio, DataContext DataClassesDataContext. O-R Visual Studio, Client Client_update(id, name). ( GUI OR Designer), Client id, - Client_update(id, name) ( ).

XAML ( MainWindow.xaml.cs):

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Loaded="Window_Loaded">
    <ListBox x:Name="myListBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=name Mode=TwoWay}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>

TextBox Text name , , TwoWay. Trigger TextBox OnLostFocus, .

- ( MainWindow.cs):

namespace Test
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Initialize the data context
            DataClassesDataContext dataContext = new DataClassesDataContext();

            //Define the query to retrieve the list of clients from the SQL Server
            IEnumerable<Client> clients = from c in dataContext.Client
                                          select c;

            /* Set the item source to be the result of the query defined earlier
             * (This also gets the query executed)
             */
            myListBox.ItemsSource = clients;
        }
    }
}

. System.InvalidOperationException System.Data.Linq.dll, " AutoSync ". .

, O-R AutoSync Always . OnUpdate, , OnInsert Never .

, , , , . ( Microsoft SQL Management Studio )

AutoSync ? , ? , , ?

PS , , Prettify, - XAML , XML # (cs), , , .

№ 1: , .

. # 2: , , . (.. Client Clients)

+3
1

dataContext . , dataContext, dataContext.SubmitChanges();. , SubmitChanges() ( , ):

DataClassesDataContext dataContext = new DataClassesDataContext();
......
private void ButtonSubmit_Click(object sender, RoutedEventArgs e)
{
    dataContext.SubmitChanges();
}

UPDATE: ( )

, LINQ to SQL. dataContext , db, SubmitChanges , db.

TwoWay -to-db db-to-application. [ UI] -to- [ ] . TextBox, name , , TextBox . , . , db , SubmitChanges .

AutoSync, this SO answer , .

0

All Articles