Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mode=TwoWay, UpdateSourceTrigger=PropertyChanged or LostFocus?

I bind a table of from a Database into a DataGrid using an 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 into my DataGrid.

I have searched about the possibility of being able to send data from my DataGrid to my Database and I found the TwoWay mode and the UpdateSourceTrigger property: http://msdn.microsoft.com/fr-fr/library/system.windows.data.binding.updatesourcetrigger(v=vs.110).aspx

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

EDIT1: This is the interesting part of 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" />
like image 476
Kraenys Avatar asked Sep 03 '14 09:09

Kraenys


1 Answers

Use of the Binding.UpdateSourceTrigger property is fairly straight forward. It affects when property changes that are made in the UI are reflected in their data bound source objects in the code behind or view models. From the UpdateSourceTrigger Enumeration page on MSDN:

LostFocus: Updates the binding source whenever the binding target element loses focus.

PropertyChanged: Updates the binding source immediately whenever the binding target property changes.

Which one you choose will depend on your requirements. If you want the Bindings and any validation that you may be using to update as the user types each character, then choose the PropertyChanged value. If you want the Bindings and any validation that you may be using to update as the user types tabs away from each control, or otherwise selects a different control, then choose the LostFocus value.

Now to clarify the use of the Binding.Mode Property, you should be aware of which direction the OneWay and OneWayToSource values work in. From the linked Mode page on MSDN:

TwoWay updates the target property or the property whenever either the target property or the 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 a change.

OneWayToSource updates the source property when the target property changes.

Default causes the default Mode value of target property to be used.

To clarify further, the target that is referred to here is the UI control and the source is the data object that is set as the data bound data source.

like image 61
Sheridan Avatar answered Oct 29 '22 20:10

Sheridan