2

Pulling my hair out here. I can't get my combobox within my listview to bind to a list in the code behind.

Also the combobox isn't even appearing within the column..

Want a combobox in listview to show numbers 0-24.

XAML:

<ListView Grid.Row="0" Margin="0,0,0,0" Height="250" Width="540" SelectionMode="Single" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" x:Name="TasksList">
    <ListView.View>
        <GridView>
            <GridViewColumn Header ="Day 1" Width="50">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=ComboBox1}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
       </GridView>
    </ListView.View>
</ListView>

And Code behind:

public partial class TaskHoursRemaining : Page {

    List<int> hourOfDay = new List<int>();

    public TaskHoursRemaining() {
        InitializeComponent();
        LoadData();
        DataContext = this;
    }

    private void LoadData() {
        for (int i = 0; i < 25; i++) {
            hourOfDay.Add(i);
        }
        this.ComboBox1.ItemsSource= hourOfDay;
    }
}

but ComboBox1 does not exist in the current context.

1
  • What you are doing wrong here is, the control is inside the DataTemplate of the List View, so you have to bind the ListView with required collection Commented Nov 30, 2016 at 1:09

1 Answer 1

4

In your XAML, you're binding to a non-existent property ComboBox1:

<ComboBox ItemsSource="{Binding Path=ComboBox1}"/>

In your code-behind, you're accessing a non-existent field ComboBox1:

this.ComboBox1.ItemsSource= hourOfDay;

The DataContext = this; statement does nothing useful for you here.

To create fields via XAML, you should use the x:Name attribute. This wouldn't help you anyway, since, the ComboBox resides in a template.

@un-lucky is correct that you should bind the list view to the collection (which is in fact what you're trying to do in your code-behind). Then again, the ComboBox also wants a collection, so you should properly have a data model that is a collection of collections. (Sort of -- all the comboboxes want the same collection; only the selected item will differ.)

Let's first make this work with a TextBox instead of a ComboBox. The list binds to hourOfDay, while the TextBox displays the int:

private readonly List<int> hourOfDay = new List<int>();

public MainWindow()
{
    InitializeComponent();

    for (int i = 0; i < 25; i++)
    {
        this.hourOfDay.Add(i);
    }

    this.TasksList.ItemsSource = this.hourOfDay;
}

XAML:

<ListView Grid.Row="0" Margin="0,0,0,0" Height="250" Width="540" SelectionMode="Single" x:Name="TasksList">
    <ListView.View>
        <GridView>
            <GridViewColumn Header ="Day 1" Width="50">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Mode=OneWay}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

Result:

enter image description here

What you want, though, is a list of somethings, where each combobox has a dropdown with 1-24. I don't know what the somethings might be -- perhaps something like this:

public class Entry
{
    private static readonly List<int> hourOfDay;

    static Entry()
    {
        hourOfDay = new List<int>();
        for (int i = 0; i < 25; i++)
        {
            hourOfDay.Add(i);
        }
    }

    public IEnumerable<int> HourOfDaySource => hourOfDay;
}

In the window/page constructor:

InitializeComponent();
this.TasksList.ItemsSource = new List<Entry>
    {
        new Entry(),
        new Entry(),
        new Entry(),
        new Entry(),
        new Entry(),
    };

XAML:

<ListView Grid.Row="0" Margin="0,0,0,0" Height="250" Width="540" SelectionMode="Single" x:Name="TasksList">
    <ListView.View>
        <GridView>
            <GridViewColumn Header ="Day 1" Width="60">
                <GridViewColumn.CellTemplate>
                    <DataTemplate DataType="wpf:Entry">
                        <ComboBox
                            ItemsSource="{Binding HourOfDaySource, Mode=OneWay}"
                            SelectedIndex="12"
                            Width="42"
                        />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

Result:

enter image description here

There's a goodly amount of plumbing required for this to become useful, but at least you've got your ComboBoxes populated...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.