wpf Mode=TwoWay,UpdateSourceTrigger=PropertyChanged 还是 LostFocus?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25640877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:26:50  来源:igfitidea点击:

Mode=TwoWay, UpdateSourceTrigger=PropertyChanged or LostFocus?

c#wpfxamldata-bindingdatagrid

提问by Kraenys

I bind a table of from a Database into a DataGrid using an Observable Collection:

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

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.

一种方式绑定效果很好,我可以看到我的表中的数据到我的 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

我搜索了能够将数据从我的 DataGrid 发送到我的数据库的可能性,我发现了 TwoWay 模式和 UpdateSourceTrigger 属性: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.

我认为执行此绑定是一个很好的解决方案,但我不确定该怎么做。UpdateSourceTrigger 对我来说最好的属性是 LostFocus、PropertyChanged 吗?这样做就足够了吗?我的代码仅适用于 OneWay,其他方式不起作用。

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

EDIT1:这是 app.config 文件中有趣的部分:

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

回答by Sheridan

Use of the Binding.UpdateSourceTriggerproperty 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 UpdateSourceTriggerEnumerationpage on MSDN:

Binding.UpdateSourceTrigger物业的使用相当简单。它会影响 UI 中所做的属性更改何时反映在它们的代码隐藏或视图模型中的数据绑定源对象中。从MSDN 上的UpdateSourceTrigger枚举页面:

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

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

LostFocus每当绑定目标元素失去焦点时更新绑定源。

PropertyChanged每当绑定目标属性更改时立即更新绑定源。

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 PropertyChangedvalue. 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 LostFocusvalue.

您选择哪一种取决于您的要求。如果您希望在Binding用户键入每个字符时更新 s 和任何可能用于更新的验证,请选择该PropertyChanged值。如果您希望Bindings 和任何验证可用于在用户键入远离每个控件的选项卡时更新,或者以其他方式选择不同的控件,然后选择该LostFocus值。

Now to clarify the use of the Binding.ModeProperty, you should be aware of which direction the OneWayand OneWayToSourcevalues work in. From the linked Modepage on MSDN:

现在要澄清Binding.ModeProperty的使用,您应该知道OneWayOneWayToSource值在哪个方向起作用。从ModeMSDN 上的链接页面:

? TwoWayupdates the target property or the property whenever either the target property or the source property changes.

? OneWayupdates the target property only when the source property changes.

? OneTimeupdates the target property only when the application starts or when the DataContext undergoes a change.

? OneWayToSourceupdates the source property when the target property changes.

? Defaultcauses the default Mode value of target property to be used.

? 每当目标属性或源属性发生更改时,TwoWay 都会更新目标属性或属性。

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

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

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

? Default导致使用目标属性的默认 Mode 值。

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

为了进一步澄清,这里所指的目标是 UI 控件,是设置为数据绑定数据源的数据对象。