2009-02-24 3 views
6

Tôi muốn tạo combo box có thể chỉnh sửa với các thuộc tính sau:Editable ComboBox

  1. Bind văn bản tài sản để mô hình dữ liệu của tôi.
  2. Mô hình dữ liệu có thể ghi đè lên các thay đổi trong GUI, ngay cả trong Lựa chọn đã thay đổi. Ví dụ. Tôi có thể chọn từ 1, 2, 3 tôi chọn 2, nhưng một số thành phần xuống dưới thay đổi nó để 3.
  3. Cập nhật mô hình dữ liệu về các sự kiện sau:

    1. Selection Changed
    2. mất tập trung
    3. Nhập được nhấn (sẽ hoạt động giống như tiêu điểm bị mất).

tôi đã có thể để tạo ra sự kiểm soát như vậy, nhưng nó khá xấu xí (sử dụng nhiều hacks) và tôi hy vọng có một cách đơn giản hơn ...

Cảm ơn trước

Trả lời

2

Ok, tôi đây là những gì tôi đã làm và không phải là xấu xí:

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

Cách đơn giản nhất để làm điều này là sử dụng thuộc tính UpdateSourceTrigger trên ràng buộc. Bạn có thể không phù hợp với hành vi hiện tại của bạn một cách chính xác, nhưng bạn có thể thấy rằng nó có thể so sánh được.

Thuộc tính UpdateSourceTrigger kiểm soát khi mục tiêu của liên kết cập nhật nguồn. Các điều khiển WPF khác nhau có các giá trị mặc định khác nhau cho thuộc tính này khi bị ràng buộc.

Dưới đây là lựa chọn của bạn:

UpdateSourceTrigger.Default = Cho phép kiểm soát mục tiêu để xác định chế độ UpdateSourceTrigger.

UpdateSourceTrigger.Explicit = Chỉ cập nhật nguồn khi ai đó gọi BindingExpression.UpdateSource();

UpdateSourceTrigger.LostFocus = Tự động cập nhật nguồn ràng buộc bất cứ khi nào mục tiêu mất tiêu điểm. Bằng cách này, một thay đổi có thể được hoàn thành, và sau đó các ràng buộc được cập nhật sau khi người dùng di chuyển trên.

UpdateSourceTrigger.PropertyChanged = Bất cứ khi nào DependencyProperty trên mục tiêu thay đổi giá trị, nguồn được cập nhật ngay lập tức. Hầu hết các UserControl không mặc định thuộc tính này bởi vì nó đòi hỏi các bản cập nhật ràng buộc nhiều hơn (có thể là một vấn đề hiệu năng).