2009-02-24 32 views
6

編集可能なコンボボックス

  1. バインド私のデータモデルへのテキストプロパティ。
  2. データモデルは、選択が変更された場合でも、GUIの変更を無効にすることがあります。例えば。私はそれが3
  3. Updateに変化の下で、次のイベントのデータモデルをダウン私は2を選択3、、1から2を選択したが、一部のコンポーネントができます。

    1. 選択は
    2. 失うフォーカス
    3. を変更しました
    4. を押します(フォーカスが失われた場合と同じように動作する必要があります)。事前に

私は、このようなコントロールを作成することができましたが、それは(多くのハックを使用して)かなり醜いだと私は簡単な方法があります期待し...

おかげ

答えて

2

[OK]を、Iここでその醜い私がやったものだ、とそのない:

/// <summary> 
/// Editable combo box which updates the data model on the following: 
/// 1. Select## Heading ##ion changed 
/// 2. Lost focus 
/// 3. Enter or Return pressed 
/// 
/// In order for this to work, the EditableComboBox requires the follows, when binding: 
/// The data model value should be bounded to the Text property of the ComboBox 
/// The binding expression UpdateSourceTrigger property should be set to LostFocus 
/// e.g. in XAML: 
/// <PmsEditableComboBox Text="{Binding Path=MyValue, UpdateSourceTrigger=LostFocus}" 
/// ItemsSource="{Binding Path=MyMenu}"/> 
/// </summary> 
public class PmsEditableComboBox : ComboBox 
{ 
    /// <summary> 
    /// Initializes a new instance of the <see cref="PmsEditableComboBox"/> class. 
    /// </summary> 
    public PmsEditableComboBox() 
     : base() 
    { 
     // When TextSearch enabled we'll get some unwanted behaviour when typing 
     // (i.e. the content is taken from the DropDown instead from the text) 
     IsTextSearchEnabled = false; 
     IsEditable = true; 
    } 

    /// <summary> 
    /// Use KeyUp and not KeyDown because when the DropDown is opened and Enter is pressed 
    /// We'll get only KeyUp event 
    /// </summary> 
    protected override void OnKeyUp(KeyEventArgs e) 
    { 
     base.OnKeyUp(e); 

     // Update binding source on Enter 
     if (e.Key == Key.Return || e.Key == Key.Enter) 
     { 
      UpdateDataSource(); 
     } 
    } 

    /// <summary> 
    /// The Text property binding will be updated when selection changes 
    /// </summary> 
    protected override void OnSelectionChanged(SelectionChangedEventArgs e) 
    { 
     base.OnSelectionChanged(e); 
     UpdateDataSource(); 
    } 

    /// <summary> 
    /// Updates the data source. 
    /// </summary> 
    private void UpdateDataSource() 
    { 
     BindingExpression expression = GetBindingExpression(ComboBox.TextProperty); 
     if (expression != null) 
     { 
      expression.UpdateSource(); 
     } 
    } 

} 
0

これを行うための最も簡単な方法は、結合にUpdateSourceTriggerプロパティを使用することです。あなたは現在の行動を正確に一致させることができないかもしれませんが、それは匹敵するかもしれません。

UpdateSourceTriggerプロパティは、バインディングのターゲットがソースを更新するときを制御します。異なるWPFコントロールには、バインドされたときにこのプロパティのデフォルト値が異なります。ここで

は、あなたのオプションは次のとおりです。

UpdateSourceTrigger.Default =ターゲット・制御モードをUpdateSourceTrigger決定できるようにします。

UpdateSourceTrigger.Explicit =誰かがBindingExpression.UpdateSource()を呼び出すときにのみソースを更新します。

UpdateSourceTrigger.LostFocus =ターゲットがフォーカスを失うたびに自動的にバインディングソースを更新します。このようにして変更を完了し、ユーザーが移動した後にバインディングが更新されます。

UpdateSourceTrigger.PropertyChanged =ターゲットのDependencyPropertyが値を変更するたびに、ソースが即時に更新されます。ほとんどのUserControlsは、より多くのバインディングの更新を必要とするため(パフォーマンス上の問題である可能性があるため)、このプロパティをデフォルトにしません。

関連する問題