C# 当绑定到itemsource WPF C时,组合框显示-1元素#
我有这样一份清单:C# 当绑定到itemsource WPF C时,组合框显示-1元素#,c#,wpf,combobox,C#,Wpf,Combobox,我有这样一份清单: List<string> test = new List<string>() { "1","2","3" }; 当我启动程序时,组合框选择了-1元素(空)。现在,当我单击它时,它将显示列表中的3个项目。但是,当我选择其中一个项目时,我不能再次将其设置为空。有没有办法让我在不编辑列表的情况下再次选择空字段 Xaml: 只需在要进行空选择的地方键入以下代码即可 comboBox1.SelectedIndex = -1; 如果未在combo ItemsS
List<string> test = new List<string>() { "1","2","3" };
当我启动程序时,组合框选择了-1元素(空)。现在,当我单击它时,它将显示列表中的3个项目。但是,当我选择其中一个项目时,我不能再次将其设置为空。有没有办法让我在不编辑列表的情况下再次选择空字段
Xaml:
只需在要进行空选择的地方键入以下代码即可
comboBox1.SelectedIndex = -1;
如果未在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>
四,。转换器代码:
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();
}
}
五,。BaseObserveObject是INotifyPropertyChanged接口的简单实现
关于,您的代码中的以下内容有什么
combobox1.DisplayMemberPath=;combobox1.WPF中SelectedValuePath的绑定与WinForms和WebForms中的绑定不同。。选中此链接,然后设置组合框1。SelectedIndex=0
FYISelectedIndex保证是唯一的,SelectedItem不理解您所说的内容,但我无法编辑列表。我希望名单保持不变。绑定到列表并没有什么问题,它确实显示了列表中的每个对象,只是当我选择了一个项目时,我希望能够取消选择一个项目。我可以制作一个按钮,将组合框设置为selecteditem=-1,但我不想这样做。也许你需要阅读一下如何在WPF中正确使用绑定这在WinForms和WebForms中效果很好。你的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>
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();
}
}
}
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();
}
}
}
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();
}
}