模式=双向,UpdateSourceTrigger =属性更改还是丢失? [英] Mode=TwoWay, UpdateSourceTrigger=PropertyChanged or LostFocus?

查看:95
本文介绍了模式=双向,UpdateSourceTrigger =属性更改还是丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用可观察的集合将数据库的表绑定到DataGrid中:

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;
}

这是我的DataGrid:

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>

单向绑定效果很好,我可以看到表中的数据进入DataGrid。

One Way binding works well, I can see data from my table into my DataGrid.

我搜索了将数据从DataGrid发送到数据库的可能性,并发现了TwoWay模式和UpdateSourceTrigger属性: http://msdn.microsoft.com/fr-fr/ library / system.windows.data.binding.updatesourcetrigger(v = vs.110).aspx

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

我认为执行此绑定是一个很好的解决方案,但我不确定该怎么办。 UpdateSourceTrigger对我来说最好的属性是LostFocus,PropertyChanged吗?
是否足以做到这一点?我的代码仅在OneWay上有效,反之则无效。

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:
这是app.config文件中有趣的部分:

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


推荐答案

使用绑定。 UpdateSourceTrigger 属性相当简单。它会影响在UI中进行的属性更改何时在其背后的代码或视图模型中的数据绑定源对象中反映出来。从 <$ c $ MSDN上的c> UpdateSourceTrigger 枚举页:

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 更新

PropertyChanged 只要绑定目标属性更改,就立即更新绑定源。

您选择哪种取决于您的要求。如果您想要 Binding s以及您在用户键入每个字符时可能用来更新的验证,则选择 PropertyChanged 值。如果您想要 Binding s以及您在用户键入每个控件之外的选项卡时可能使用的验证,或者选择其他控件,则选择 LostFocus 值。

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.

现在要弄清 Binding.Mode 属性 ,您应该知道 OneWay OneWayToSource 值的作用方向。从链接的模式 页面:

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 更新目标属性或只要该属性目标属性或源属性更改。

TwoWay updates the target property or the property whenever either the target property or the source property changes.

OneWay 仅在源属性更改时更新目标属性。

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

OneTime 仅在应用程序启动或DataContext进行更改时更新目标属性。

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

OneWayToSource 在目标属性更改时更新源属性。

OneWayToSource updates the source property when the target property changes.

默认导致默认模式要使用的目标属性的值。

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

为进一步说明,此处引用的 target 是UI控件, source 是设置为数据绑定数据源的数据对象。

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.

这篇关于模式=双向,UpdateSourceTrigger =属性更改还是丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆