【问题标题】:WPF:SelectedItem Binding at ComboBoxWPF:ComboBox 处的 SelectedItem 绑定
【发布时间】:2014-04-24 00:25:07
【问题描述】:

我在 wpf 和 xaml 中的 ComboBox 有问题。我的组合框的 ItemsSource 是一个自定义类 (Contacts.cs)。如果我输入一个字符串(长度 > 2),它 DropDownList 应该只包含包含我输入的子字符串的项目。这很好用。但知道我还有其他问题。我用一个例子来解释:

  1. 选择一项 (Testi2)
  2. 删除最后一个字符 (Testi)
  3. 下拉菜单打开 itemssource 显示所有包含“Testi”的项目
  4. 文本框显示 Testi2
  5. 预期结果:文本框显示 Testi

我认为 SelectedItem 有问题,但我不知道如何解决。

组合框行为的图像 http://postimg.org/image/dl2s4iggn/

Contacts.cs

namespace Test.Model
{
class Contacts
{
    private string name;
    private string age;

    public Contacts(string name, string age)
    {
        this.name = name;
        this.age = age;
    }

    public Contacts()
    {
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public string Age
    {
        get { return age; }
        set { age = value; }
    }
}
}

MainWindow.xaml

<custom:CustomComboBox x:Name="CustomCombo"
   Grid.Row="1" 
   Grid.Column="1"
   ItemsSource="{Binding CustomContacts}"
   SelectedItem="{Binding CustomSelectedContact}" 
   DisplayMemberPath="Name"
   Margin="5"
   IsEditable="True"
   IsTextSearchEnabled="False"
   KeyUp="CustomComboBox_KeyUp"/>
<TextBlock Grid.Row="1"
   Grid.Column="2"
   Margin="10,5,5,0”
   VerticalAlignment="Center">
          <Run Text="{Binding CustomSelectedContact.Name}"/>
          <Run Text=" - "/>
          <Run Text="{Binding CustomSelectedContact.Age}"/>
</TextBlock>

MainWindow.caml.cs

namespace Test.View
{
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var viewModel = new MainWindowViewModel();
        DataContext = viewModel;
    }

    private void CustomComboBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (CustomCombo.Text.Length < 3)
            return;

        var viewModel = (MainWindowViewModel)DataContext;
        viewModel.CustomSearchParameter = CustomCombo.Text;
        CustomCombo.IsDropDownOpen = true;
    }

}
}

MainWindowViewModel.cs

namespace Test.ViewModel
{
class MainWindowViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Contacts> customcontacts;
    private Contacts customselectedContact;
    private string customsearchParameter;

    public MainWindowViewModel()
    {
        customcontacts = new ObservableCollection<Contacts>();
        customcontacts.Add(new Contacts("Test", "asdf"));
        customcontacts.Add(new Contacts("Testi1", "iasdf"));
        customcontacts.Add(new Contacts("Testi2", "ijkl"));
        customcontacts.Add(new Contacts("Testo", "o"));
        customcontacts.Add(new Contacts("Testo2", "oasdf"));
        customcontacts.Add(new Contacts("Other", "otherasdf"));
        customselectedContact = new Contacts();
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public ObservableCollection<Contacts> CustomContacts
    {
        get
        {
            if (!String.IsNullOrEmpty(CustomSearchParameter))
            {
                ObservableCollection<Contacts> temp = new ObservableCollection<Contacts>();
                foreach (Contacts contact in customcontacts)
                {
                    if (contact.Name.Contains(CustomSearchParameter))
                    {
                        temp.Add(contact);
                    }
                }
                return temp;
            }
            return customcontacts;
        }
        set { customcontacts = value; }
    }

    public Contacts CustomSelectedContact
    {
        get { return customselectedContact; }
        set
        {
            customselectedContact.Name = value.Name;
            customselectedContact.Age = value.Age;
            OnPropertyChanged("CustomSelectedContact");
        }
    }

    public string CustomSearchParameter
    {
        get { return customsearchParameter; }
        set
        {
            customsearchParameter = value;
            OnPropertyChanged("CustomContacts");
        }
    }
}
}

CustomComboBox.cs

namespace Test.CustomWindowsControls
{
class CustomComboBox : ComboBox
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

            this.DropDownOpened += comboBox_DropDownOpened;
    }

    void comboBox_DropDownOpened(object sender, EventArgs e)
    {
        var element = GetTemplateChild("PART_EditableTextBox");
        if (element != null)
        {
            var txt = (TextBox)element;

            if (base.IsDropDownOpen && txt.SelectionLength > 0)
            {
                txt.CaretIndex = base.Text.Length;
            }
        }
    }
}
}

【问题讨论】:

    标签: c# wpf xaml data-binding combobox


    【解决方案1】:

    您正在将 SelectedItem 属性绑定到“customselectedContact”,它表示组合中当前选定的对象。当最终用户过滤列表时,会选择一个对象,因此文本框的文本会发生变化。

    快速解决方法是:

    Text="{Binding CustomSearchParameter}"
    

    【讨论】:

    • 太棒了。谢谢你。我解决了 CustomCombo.SelectedIndex = -1;CustomCombo.Text = viewModel.CustomSearchParameter;
    猜你喜欢
    • 1970-01-01
    • 2012-02-12
    • 2012-08-26
    • 2019-11-10
    • 2023-04-03
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多