2011-04-20 50 views
0

我有在OneWayToSource不希望的源更新的問題,UpdateSourceTrigger.Explicit場景WPF結合(的數據網格)OneWayToSource,UpdateSourceTrigger-顯 - 不希望的源更新

背景: 我有一個包含有用戶數據的數據網格密碼欄。 我已衍生自DataGridTemplateColumn類DataGridPasswordColumn到

顯示在非編輯模式一些虛擬屏蔽的數據(例如,####) 這是通過設置CellTemplate以恆定的值一個TextBlock完成:

FrameworkElementFactory frameworkElementFactory = new FrameworkElementFactory(typeof(TextBlock)); 
frameworkElementFactory.SetValue(TextBlock.TextProperty, Properties.Resources.passwordEncrypted); 
CellTemplate = new DataTemplate { VisualTree = frameworkElementFactory }; 

和顯示兩個PasswordBox控制和在編輯模式中的OK按鈕被用下面的代碼:

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,並等待用戶點擊OKButton,然後做一些驗證(是兩個PasswordBoxes的內容是否一致?)。如果驗證無誤,請通過以下代碼調用UpdateSource:

    BindingExpression be = this.GetBindingExpression(SelectedPasswordProperty);如果(be!= null) { 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錯誤? 有沒有人有同樣的問題?

回答

0

我發現使用TwoWay綁定可以解決我的問題,並且不會執行意外的源更新。但我仍然不明白爲什麼這個初始目標來源更新完成。我認爲這有一個技術原因。