简体   繁体   中英

Why isn't this combobox displaying items?

I'm writing an import utility, I'd like for users to be able to upload from a CSV and then direct the columns where they'd like them to go.

So in order to do this, I'm making the header of each column a ComboBox populated with all of the possible columns.

xaml

        <DataGrid x:Name="ImportTable" 
                  ItemsSource="{Binding displayTable}"
                  AutoGeneratingColumn="OnAutoGeneratingColumn"
                  AutoGenerateColumns="True"
                  CanUserAddRows="True" 
                  CanUserDeleteRows="True"
                  EnableColumnVirtualization="True"
                  EnableRowVirtualization="True"
                  MaxWidth="1300"
                  MaxHeight="600"
                  />

xaml.cs

    private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {

        var cb = new ComboBox();
        foreach (DataColumn test in (DataContext as EnterValueDialogViewModel).displayTable.Columns)
            Console.Out.WriteLine(test);

        cb.ItemsSource = (DataContext as EnterValueDialogViewModel).displayTable.Columns;
        e.Column.Header = cb;
    }

This correctly prints out all of the columns, but nothing is actually displayed inside of the combo box's

Empty Combobox


The combobox now displays properly in the dropdown. but i can't get it's selectedValue to set. The following code prints out that the selectedvalue is correct, but it's still populating initially as blank/unselected

        var cb = new ComboBox();
        cb.DisplayMemberPath = "ColumnName";
        cb.SelectedValue = e.PropertyName.ToString();
        cb.ItemsSource = (DataContext as EnterValueDialogViewModel).displayTable.Columns;
        Console.Out.WriteLine(cb.SelectedValue);
        e.Column.Header = cb;

You can try to set the DisplayMemberPath of your combobox.

    private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {

        var cb = new ComboBox();
        cb.DisplayMemberPath = "SomePropertyFromYourCollection";
        foreach (DataColumn test in (DataContext as EnterValueDialogViewModel).displayTable.Columns)
            Console.Out.WriteLine(test);

        cb.ItemsSource = (DataContext as EnterValueDialogViewModel).displayTable.Columns;
        e.Column.Header = cb;
    }

Documentation here .

You need to set the DisplayMemberPath and the SelectedValuePath of the combo box I think.

These will be column names from your data file.

It should look something like this:

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var cb = new ComboBox();
    foreach (DataColumn test in (DataContext as EnterValueDialogViewModel).displayTable.Columns)
        Console.Out.WriteLine(test);

    cb.ItemsSource = (DataContext as EnterValueDialogViewModel).displayTable.Columns;
    cb.DisplayMemberPath = "Column1";
    cb.SelectedValuePath = "Column2";
    e.Column.Header = cb;
}   

Let me know if it helped.

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