locked
WPF, ADO.NET and two-way data binding RRS feed

  • Question

  • In WPF I have a list box, which contains some text boxes, the text in
    the boxes are populated from a database table. When the user edits the
    text, I'd like the updates to be written back to the database.

    The TableAdapter code to talk to the database is generated by
    the dataset designer in VC# Express 2008, the database is
    Microsoft SQL Server Compact 3.5 (.NET Framework Data Provider for Microsoft SQL Server Compact 3.5).
    The default binding on a text box is two-way and the update activates when the text box looses
    focus. The data is provided through an ObjectDataProvider, which is
    actually a TableAdapter. The only class I have that talks to the
    database is the auto-generated TableAdapter one, which appears to be
    capable of performing the updates. The binding from source to target
    works fine. Target to source though is another matter.

    Using diagnostics I can see that the update goes back to some kind of
    source...

    System.Windows.Data Warning: 85 : BindingExpression (hash=62910583): Got LostFocus event from TextBox (hash=29197104)
    System.Windows.Data Warning: 79 : BindingExpression (hash=62910583): Update - got raw value 'Tom has changed'
    System.Windows.Data Warning: 84 : BindingExpression (hash=62910583): Update - using final value 'Tom has changed'
    System.Windows.Data Warning: 92 : BindingExpression (hash=62910583): SetValue at level 0 to DataRowView (hash=24901833) using DataColumnPropertyDescriptor(Name): 'Tom has changed'
    System.Windows.Data Warning: 85 : BindingExpression (hash=62910583): Got PropertyChanged event from DataRowView (hash=24901833)

    yet the database is not updated with the new value : 'Tom has
    changed'. The TableAdapter seemingly has the ability to perform the
    update, but it does not get called. How can I get this data written
    to the database?

    Thanks

    Adrian

    Here is the XAML (the rest of the source is available):

    <Window x:Class="WPFandDataSetDesigner.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tableAdapters="clr-namespace:WPFandDataSetDesigner.DataSet1TableAdapters"
        xmlnsBig Smileiagnostics="clr-namespaceTongue Tiedystem.Diagnostics;assembly=WindowsBase"
        Title="WPF and Data Set Designer" Height="300" Width="300">
        <Window.Resources>
            <ObjectDataProvider
                x:Key="NamesTableAdapter"
                ObjectType="{x:Type tableAdapters:NamesTableAdapter}"
                IsAsynchronous="True"
                MethodName="GetData" />
            <DataTemplate x:Key="NamesTemplate">
                <TextBox Text="{Binding Path=Name,
                         diagnosticsStick out tongueresentationTraceSources.TraceLevel=High}" />
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ListBox Name="NamesListBox"
                     Grid.Row="0"
                     Grid.Column="0"
                     ItemsSource="{Binding Source={StaticResource NamesTableAdapter} }"
                     ItemTemplate="{StaticResource NamesTemplate}"
                     IsSynchronizedWithCurrentItem="True" />
        </Grid>
    </Window>

    Sunday, March 16, 2008 9:49 AM

Answers