ComboBox SelectedItem = null with DisplayMemberPath

本文关键字:with DisplayMemberPath null SelectedItem ComboBox | 更新日期: 2023-09-27 18:17:05

在WPF组合框中,当使用DisplayMemberPath时,如何使ItemsSource中的空实例可选?

我绑定到一个对象集合,并将null添加到该集合。我还绑定了SelectedItem属性。当选择非空值时,我的SelectedItem属性正确更改。但是当我从下拉列表中选择空项时,我的SelectedItem属性不改变

我已经把它隔离到DisplayMemberPath的使用,也就是说,它不会发生与字符串的集合,因为你不会使用DisplayMemberPath。

这对我来说都是有意义的,因为我的对象中没有DisplayMemberPath是空白或null的实例,列表中只有一个空项。然而,我正试图找到最好的方法来提供一种方式空白的组合框。

我想我可以创建一个对象的实例,其中DisplayMemberPath属性的值是一个空白字符串,但这对我来说似乎有点黑客。

有什么建议吗?

我的例子如下:

这是我填充ItemsSource的类:

public class Car
{
    public string Make { get; set; }
    public Car(string make)
    {
        Make = make;
    }
}

这是我的xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ComboBox SelectedItem="{Binding MyCar}" ItemsSource="{Binding MyCars}" DisplayMemberPath="Make" />
</Grid>

下面是我的代码:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private Car myCar = null;
    public List<Car> MyCars { get; set; }
    public Car MyCar
    {
        get { return myCar; }
        set
        {
            if (myCar != value)
            {
                myCar = value;
                OnPropertyChanged("MyCar");
            }
        }
    }

    public MainWindow()
    {
        MyCar = null;
        MyCars = new List<Car>();
        MyCars.Add(null);
        MyCars.Add(new Car("Ford"));
        MyCars.Add(new Car("Chevy"));
        MyCars.Add(new Car("Toyota"));
        InitializeComponent();
    }

    #region INotifyPropertyChanged
    /// <summary>
    /// Occurs when a property value changes.
    /// </summary>
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// This function is used to raise an PropertyChanged event.
    /// </summary>
    /// <param name="prop">The name of the property whose value changed.</param>
    internal void OnPropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(prop));
        }
    }
    #endregion
}

ComboBox SelectedItem = null with DisplayMemberPath

添加(在VB中,所以你需要转换)一个

Public Overrides Function ToString() As String
    If string.isnullorwhitespace(Make) then
        return "no Car"
    else
        return make
    end if
End Function

到你的汽车类,那么就不需要使用displaymemberpath,看看它是否像字符串的itemssource

 private string[] Logo_menu_array = { "Assets/star-6-48.ico", "/Assets/note-48.ico", "/Assets/medal-48.ico", "/Assets/joystick-48.ico" };
    private string[] Text_menu_array={"Phổ biến trên YouTuBe","Âm nhạc","Thể thao","Trò chơi"};



    public class listboxitem
    {
        public string textmenu { get; set; }
        public string logomenu { get; set; }
    }

public List<listboxitem> Load_Menu()
    {
        List<listboxitem> text = new List<listboxitem>();
        for (int i = 0; i < Math.Min(Logo_menu_array.Length, Text_menu_array.Length); i++)
        {
            var l = new listboxitem();
            l.logomenu = Logo_menu_array[i];
            l.textmenu = Text_menu_array[i];
            text.Add(l);
        }
        return text;
    }
public MainPage()
    {
        this.InitializeComponent();
        //get menu
         List<listboxitem> menu_list = Load_Menu();
         lst_menu.ItemsSource = menu_list;

    }