Mode = TwoWay, UpdateSourceTrigger = PropertyChanged or LostFocus?

I bind a table from the database to the DataGrid using the Observable collection:

class ViewModel:INotifyPropertyChanged
{
    private BDDInterneEntities _BDDInterneEntities;

    public ViewModel()
    {
        _BDDInterneEntities = new BDDInterneEntities();
        ResultatCollection = new ObservableCollection<Resultat>(_BDDInterneEntities.Resultat);

    }         
    public ObservableCollection<Resultat> ResultatCollection { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
}

      

This is my DataGrid:

<DataGrid x:Name="DonneesBrutes" ItemsSource="{Binding Path=.ResultatCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10,65,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="PMRQ" Width="*" Binding="{Binding Path=.TOTMPMRQ, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Header="PMRQ"></DataGridTextColumn>
        <DataGridTextColumn x:Name="LibellePMRQ" Width="*" Binding="{Binding Path=.LibelléTOTApres, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Header="Libellé PMRQ"></DataGridTextColumn>
        <DataGridTextColumn x:Name="Ligne" Width="*" Header="Ligne"></DataGridTextColumn>
        <DataGridTextColumn x:Name="OTM" Width="*" Header="OTM"></DataGridTextColumn>
        <DataGridTextColumn x:Name="TOTM" Width="*" Header="TOTM"></DataGridTextColumn>
        <DataGridTextColumn x:Name="LibelleTOTM" Width="*" Header="Libellé OTM"></DataGridTextColumn>
        <DataGridTextColumn x:Name="GA" Width="*" Header="GA"></DataGridTextColumn>
        <DataGridTextColumn x:Name="Discipline" Width="*" Header="Discipline"></DataGridTextColumn>
        <DataGridTextColumn x:Name="DisciplineSubstituee" Width="120" Header="Discipline Substituée"></DataGridTextColumn>
        <DataGridTextColumn x:Name="colonnesupp" Width="*" Header="colonne supp"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

      

One way binding works well, I can see data from my table in my DataGrid.

I was looking for a way to send data from my DataGrid to my database and I found TwoWay mode and UpdateSourceTrigger property: http://msdn.microsoft.com/fr-fr/library/system.windows.data.binding.updatesourcetrigger(v= vs.110) .aspx

I think this is a great solution to accomplish this binding, but I'm not sure what to do. Is the UpdateSourceTrigger the best property for me LostFocus, PropertyChanged? Is it enough to do this? My code only works on OneWay, the other way doesn't work.

EDIT1: This is the interesting part of the app.config file:

<connectionStrings>
    <add name="BDDInterneEntities" connectionString="metadata=res://*/ModelBddInterne.csdl|res://*/ModelBddInterne.ssdl|res://*/ModelBddInterne.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\BDDInterne.mdf;integrated security=true;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.entityClient" />

      

+3


source to share


1 answer


Using the property is Binding.UpdateSourceTrigger

pretty straight forward. This is affected when changes to properties that are made in the user interface are reflected in their data objects, in code or view models. From the UpdateSourceTrigger

Enumeration
page on MSDN:

LostFocus : Updates the anchor source whenever the anchor target loses focus.

PropertyChanged : Immediately updates the binding source when the binding property of the binding changes.

Which one you choose will depend on your requirements. If you want Binding

and any validation that you can use to update as the user enters every character, select value PropertyChanged

. If you want Binding

and any validation that you can use to update when the user enters tabs separately from each control, or otherwise selects a different control, choose a value LostFocus

.



Now, to clarify the use of the properties Binding.Mode

, you need to know the values of work in which direction OneWay

and OneWayToSource

. linked page Mode

on MSDN:

TwoWay updates the target or property when the target or source property changes.

OneWay updates the target property only when the source property changes.

OneTime updates the target property only when the application starts or when the DataContext undergoes changes.

OneWayToSource updates the source property when the target property changes.

Default causes the default to be used for the target property.

To clarify, the target referred to here is a user interface control and the source is a data object that is set as a data-bound data source.

+7


source







All Articles