当绑定到itemsource WPF C#

时间:2015-11-27 21:01:20

标签: c# wpf combobox

我有一个这样的清单:

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

2 个答案:

答案 0 :(得分:0)

只需键入以下代码即可进行空选择。

comboBox1.SelectedIndex = -1;

答案 1 :(得分:0)

如果没有在combo 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. 外观如何:selecteddeleted
  2. 如果我的代码出现问题,我会很乐意提供帮助。 的问候,