Mode=TwoWay, UpdateSourceTrigger=PropertyChanged or LostFocus?

做~自己de王妃 提交于 2020-11-28 08:22:19

问题


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" />

回答1:


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.



来源:https://stackoverflow.com/questions/25640877/mode-twoway-updatesourcetrigger-propertychanged-or-lostfocus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!