简体   繁体   中英

WPF combobox bind with collections displayvaluepath,SelectedValuePath not working

Problem in WPF ComboBox item:

XAML:

<ComboBox x:Name="cboSelectSeries"  Width="100" Height="25" Grid.Row="3"
          Grid.Column="3" SelectedIndex="0"
          ItemsSource="{Binding}" SelectedValuePath="SeriesNumber" 
          DisplayMemberPath="NId" />

XAML.cs

internal List<NPIS.PortableObject.NPIS> NCollection;
..
NCollection=getdata();
cboSelectSeries.DataContext = NCollection;

Output:

with items as "NPIS.PortableObject.NPIS"

I suspect it could not find property path NId in class NPIS . Make sure you have public exposed property in underlying class. Also path name is case sensitive , make sure path name and property name is correct.

public int NId { get; set; }

I tried the following code and it did worked.

public partial class MainWindow : Window
{
    public List<PortableObject> NCollection;

    public MainWindow()
    {
        InitializeComponent();

        NCollection = getdata();
        cboSelectSeries.DataContext = NCollection;
    }

    private List<PortableObject> getdata()
    {
        return new List<PortableObject>
                           {
                               new PortableObject
                                   {
                                       SeriesNumber = 001,
                                       NId = 10,
                                   },
                                    new PortableObject
                                   {
                                       SeriesNumber = 002,
                                       NId = 20,
                                   },
                                    new PortableObject
                                   {
                                       SeriesNumber = 003,
                                       NId = 30,
                                   },
                           };
    }
}

public class PortableObject
{
    public int SeriesNumber { get; set; }
    public int NId { get; set; }
}

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