【问题标题】:ComboBox show -1 element when binded to itemsource WPF C#组合框在绑定到 itemssource WPF C# 时显示 -1 元素
【发布时间】:2015-11-27 21:01:20
【问题描述】:

我有一个这样的列表:

List<string> test = new List<string>() { "1","2","3" };

我将一个组合框链接到它:

combobox1.ItemsSource = test;

当我启动程序时,组合框选择了 -1 元素(空)。现在,当我单击它时,它将很好地显示列表中的 3 个项目。但是当我选择其中一项时,我无法再次将其清空。有什么方法可以让我在不编辑列表的情况下再次选择空白字段?

Xaml:

<ComboBox x:Name="combobox1" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="573,26,0,0" VerticalAlignment="Top" Width="171" FontSize="16" FontWeight="Normal" Height="32"/>

【问题讨论】:

  • 你的代码中有什么combobox1.DisplayMemberPath = ; combobox1.SelectedValuePath WPF 中的 BInding 和 WinForms 和 WebForms 是不同的.. 检查这个链接 stackoverflow.com/questions/32166772/…
  • 然后设置combobox1.SelectedIndex = 0 FYI SelectedIndex is guaranteed to be unique, SelectedItem is not
  • 不太明白你在说什么,但不,我无法编辑列表。我希望列表保持不变。绑定到列表没有任何问题,它确实显示了列表中的每个对象,只是我希望能够在我已经选择一个项目时取消选择一个项目。我可以制作一个按钮,将组合框设置为 selecteditem = -1,但我不想那样做。
  • 也许您需要了解如何使用 Binding Properly in WPF 这在 WinForms 和 WebForms 中运行良好。你的 Xaml 是什么样的......
  • 将您的代码放入 cmets 更新/编辑您的问题并将其粘贴到那里对我们没有好处

标签: c# wpf combobox


【解决方案1】:

只需在要进行空选择的地方键入以下代码。

comboBox1.SelectedIndex = -1;

【讨论】:

    【解决方案2】:

    如果没有在组合 ItemsSource 集合中定义此值,则无法选择空值。但是,您可以使您的组合可编辑,然后删除选定的值。这是代码: 1.Xaml代码:

    Window x:Class="ComboBoxSoHelpAttempt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        xmlns:comboBoxSoHelpAttempt="clr-namespace:ComboBoxSoHelpAttempt"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <comboBoxSoHelpAttempt:Null2StringConverter x:Key="Null2StringConverter" />
    </Window.Resources>
    <Window.DataContext>
        <comboBoxSoHelpAttempt:ComboViewModel/>
    </Window.DataContext>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <ComboBox ItemsSource="{Binding ComboItems}" HorizontalAlignment="Center" VerticalAlignment="Center" 
                  IsEditable="True" SelectedValue="{Binding ComboSelectedValue}" 
                  DisplayMemberPath="Name" SelectedValuePath="Id" Width="150"/>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="Tomato"
            Text="{Binding ComboSelectedValue, Mode=Default, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource Null2StringConverter}}"/>
    </StackPanel></Window>
    

    2。查看模型:

     public class ComboViewModel:BaseObservableObject
    {
        private string _comboSelectedValue;
    
        public ComboViewModel()
        {
            ComboItems = new ObservableCollection<ComboItem>
            {
                new ComboItem {Name = "Bob", Id = "1"},
                new ComboItem {Name = "Joe", Id = "2"},
                new ComboItem {Name = "John", Id = "3"},
                new ComboItem {Name = "Roi", Id = "4"},
            };
        }
    
        public ObservableCollection<ComboItem> ComboItems { get; set; }
    
        public string ComboSelectedValue
        {
            get { return _comboSelectedValue; }
            set
            {
                _comboSelectedValue = value;
                OnPropertyChanged();
            }
        }
    }
    

    3。型号:

     public class ComboItem:BaseObservableObject
    {
        private string _name;
        private string _id;
    
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged();
            }
        }
    
        public string Id
        {
            get { return _id; }
            set
            {
                _id = value;
                OnPropertyChanged();
            }
        }
    }
    

    4。转换器代码:

    public class Null2StringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? "Null" : value.ToString();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    5。 BaseObservableObject 是 INotifyPropertyChanged 接口的简单实现。

    1. 它的样子:

    如果我的代码出现问题,我很乐意提供帮助。 问候,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-19
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      相关资源
      最近更新 更多