WPF 绑定 (dataGrid) OneWayToSource、UpdateSourceTrigger-Explicit - 不需要的源更新
我在 OneWayToSource、UpdateSourceTrigger.Explicit 场景中遇到不需要的源更新问题
背景: 我有一个包含用户数据和密码列的 DataGrid。 我从 DataGridTemplateColumn 派生了类 DataGridPasswordColumn 以
在非编辑模式下显示一些虚拟屏蔽数据(例如####) 这是通过将 CellTemplate 设置为具有常量值的 TextBlock 来完成的:
FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(TextBlock));
frameworkElementFactory.SetValue(TextBlock.TextProperty, Properties.Resources.passwordEncrypted);
CellTemplate = new DataTemplate { VisualTree = frameworkElementFactory };
并在编辑模式下显示两个 PasswordBox 控件和一个“确定”按钮,使用以下代码:
FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(PasswordEntry));
Binding bindingPassword = new Binding(propertyNamePassword)
{
Mode = BindingMode.OneWayToSource,
// we only want the target to source binding get activated on explicit request (user clicks on 'OK' button)
UpdateSourceTrigger = UpdateSourceTrigger.Explicit,
// we need to catch and prevent the undesired source update
NotifyOnSourceUpdated = true
};
frameworkElementFactory.SetBinding(PasswordEntry.SelectedPasswordProperty, bindingPassword);
CellEditingTemplate = new DataTemplate { VisualTree = frameworkElementFactory };
PasswordEntry 是一个用户控件,它
- 具有名为 'SelectedPasswordProperty' < 的 DependencyProperty
- < p>并等待用户单击“确定”按钮,然后进行一些验证(两个密码框的内容是否相同?)。如果验证正常,则通过以下代码调用 UpdateSource
BindingExpression = this.GetBindingExpression(SelectedPasswordProperty); 如果(是!=空) { be.UpdateSource(); }
就可以了。
问题是,当打开单元格编辑模板(PasswordEntry UserControl)时,会出现一个不需要的源更新,其值为“NULL”。
我预计当使用 UpdateSourceTrigger = UpdateSourceTrigger.Explicit 时,除非调用 UpdateSource() ,否则不会进行源更新。
到目前为止我还没有找到办法抑制这个源更新。 我尝试
NotifyOnSourceUpdated = true
过
private void PasswordEntry_SourceUpdated(object sender, DataTransferEventArgs dataTransferEventArgs)
{
...
// Cancel this source update
dataTransferEventArgs.Handled = true;
}
,但它不起作用,即源仍然更新(具有 NULL 值)。
这是 WPF 错误吗? 有人遇到过同样的问题吗?
I have a problem with an undesired source update in the OneWayToSource, UpdateSourceTrigger.Explicit scenario
Background:
I have a DataGrid containing user data with a password column.
I have derived the class DataGridPasswordColumn from DataGridTemplateColumn to
Display some dummy masked data in the non-edit mode (e.g. ####)
This is done by setting the CellTemplate to a TextBlock with a constant value:
FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(TextBlock));
frameworkElementFactory.SetValue(TextBlock.TextProperty, Properties.Resources.passwordEncrypted);
CellTemplate = new DataTemplate { VisualTree = frameworkElementFactory };
and to display two PasswordBox controls and an OK-button in the edit mode the following code is used:
FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(PasswordEntry));
Binding bindingPassword = new Binding(propertyNamePassword)
{
Mode = BindingMode.OneWayToSource,
// we only want the target to source binding get activated on explicit request (user clicks on 'OK' button)
UpdateSourceTrigger = UpdateSourceTrigger.Explicit,
// we need to catch and prevent the undesired source update
NotifyOnSourceUpdated = true
};
frameworkElementFactory.SetBinding(PasswordEntry.SelectedPasswordProperty, bindingPassword);
CellEditingTemplate = new DataTemplate { VisualTree = frameworkElementFactory };
PasswordEntry is a user control that
- Has a DependencyProperty named 'SelectedPasswordProperty'
and waits for the user to click the OKButton and then does some validation (are the contents of the two PasswordBoxes identical?). If validation is fine, calls UpdateSource via the following code
BindingExpression be = this.GetBindingExpression(SelectedPasswordProperty);
if (be != null)
{
be.UpdateSource();
}
Updating the source is fine.
The problem is, that when the cell editing template (the PasswordEntry UserControl) is opened there is one undesired source update carrying a value of 'NULL'.
I expected that when UpdateSourceTrigger = UpdateSourceTrigger.Explicit is used, there is no source update unless UpdateSource() is called.
I have so far found no way to suppress this source update.
I tried
NotifyOnSourceUpdated = true
with
private void PasswordEntry_SourceUpdated(object sender, DataTransferEventArgs dataTransferEventArgs)
{
...
// Cancel this source update
dataTransferEventArgs.Handled = true;
}
but it did not work, i.e. the source was still updated (with a NULL value).
Is this a WPF bug?
Has anybody had the same problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现使用 TwoWay 绑定解决了我的问题,并且不会进行意外的源更新。然而,我仍然不太明白为什么要完成这个初始目标到源更新。我认为这有技术原因。
I found out that using TwoWay binding solves my problem and does not do the unexpected source update. I still however do not really understand why this initial target to source update is done. I presume there is a technical reason for this.