简体   繁体   中英

WPF ComboBox Binding Doesn't work

I'm learning WPF and I have a problem. I try to initialize the combobox data while the form is loaded, but i got nothing. Here's my code:

xaml:

<Menu x:Name="mn_MainMenu" HorizontalAlignment="Left" Height="35" VerticalAlignment="Top" Width="518">
    <ComboBox Name="cbm_Menu" SelectedIndex="0" Width="340">
    </ComboBox>
    <Button HorizontalAlignment="Right" Content="Demo" Width="50"/>
    <Button HorizontalAlignment="Right" Content="Source Code" Width="80"/>
</Menu>

xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        InitCbxData(StaticVariable.ComboBoxData);
    }

    private void InitCbxData(string[] initStrings)
    {
        var comboBoxData = new ComboBoxData(initStrings);
        this.cbm_Menu.ItemsSource = comboBoxData.Items;
        this.cbm_Menu.DisplayMemberPath = "Name";
        this.cbm_Menu.SelectedValuePath = "Id";
        this.cbm_Menu.SelectedValue = "1";
    }
}

Here's a screeshot that while im debugging:

在此处输入图片说明

Update:

 public class ComboBoxData:ItemCollection
    {
        public ComboBoxData(params string[] initStrings) : base(initStrings) { }
        public ComboBoxData(string initString, char[] delimiters) : base(initString, delimiters) { }
    }


public class Item
{
    public int Id;
    public string Name;

    public Item(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}


 public class ItemCollection : IEnumerable
{
    public Item[] Items;
    public int Ctr = 0;

    public ItemCollection(params string[] initStrings)
    {
        this.Items = new Item[initStrings.Count()];
        foreach (var s in initStrings)
        {
            this.Items[Ctr] = new Item(Ctr++, s);
        }
    }

    public ItemCollection(string initString, char[] delimiters)
    {
        string[] stringElements = initString.Split(delimiters);
        foreach (var s in stringElements)
        {
            this.Items[Ctr++] = new Item(Ctr, s);
        }
    }



    public IEnumerator GetEnumerator()
    {
        return new ItemCollectionEnumerator(this);
    }
}

public class ItemCollectionEnumerator : IEnumerator
{
    public int position = -1;
    public ItemCollection itemCollection;
    public ItemCollectionEnumerator(ItemCollection itemCollection)
    {
        this.itemCollection = itemCollection;
    }

    public object Current
    {
        get { return itemCollection.Items[position]; }
    }

    public bool MoveNext()
    {
        if (position < itemCollection.Items.Length - 1)
        {
            position++;
            return true;
        }
        else
        {
            return false;
        }
    }

    public void Reset()
    {
        position = -1;
    }
}
  private void InitCbxData(string[] initStrings)
    {
        var comboBoxData = new ComboBoxData(initStrings);
        this.cbm_Menu.ItemsSource = comboBoxData.Items;
        this.cbm_Menu.DisplayMemberPath = "Name";
        this.cbm_Menu.SelectedValuePath = "Id";
        this.cbm_Menu.SelectedValue = "1";
    }

I guess that this would never work. First you receive an array of strings, so, no "Name" or "Id" is available for de DisplayMemberPath and SelectedValuePath.

The only thing it that seems to me that you have to do is

this.cbm_Menu.ItemsSource  = initstrings;

DisplayMemberPath and selectedValuePath should be used for objects that have an ID and Name as properties....(for your scenario...)

Question Solved!

Just need to modify the public field to property in the class of Item. I don't really know why, but it worked!

Modify to :

    public class Item
{
    public int Id {get;set;}
    public string Name {get;set;}

    public Item(int id, string name)
    {
        this.Id = id;
        this.Name = name;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM