2
votes

I have a listview with a few columns and a button which loads a new window to input data and submit the data to the listview. There is a screenshot below. http://img16.imageshack.us/img16/2597/databinding.jpg

I'm only trying to add one item to the listview when the add button is clicked

<ListView x:Name="StepsListView" Height="100" ItemsSource="{Binding Source currentStep}">
       <ListView.View>
          <GridView>
            <GridViewColumn Header="Priority" Width="50" DisplayMemberBinding="{Binding Path=stepPriority}"></GridViewColumn>
            <GridViewColumn Header="Description" Width="185" DisplayMemberBinding="{Binding Path=stepDescription}"></GridViewColumn>
            <GridViewColumn Header="Time" Width="50" DisplayMemberBinding="{Binding Path=stepTime}"></GridViewColumn>
            <GridViewColumn Header="Dep" Width="50" DisplayMemberBinding="{Binding Path=stepDependency}"></GridViewColumn>
            <GridViewColumn Header="Type" Width="100" DisplayMemberBinding="{Binding Path=stepType}"></GridViewColumn>
          </GridView>
       </ListView.View>
</ListView>

this is my xaml code. Please note i am completely new to WPF and the "binding" code i'm using are just little snippets from around the web. I've tried a few different ways of doing it and I'm quite confused now.

I have a data object called StepItem. Priority should be the item number in the listview so the first item is priority 1.

public class StepItem
{
    public string stepDescription { get; set; }
    public int stepTime { get; set; }
    public int stepDependency { get; set; }
    public int stepPriority { get; set; }
    public string stepType { get; set; }
}

The step button code is below. currentStep is a list of step items

 private void button1_Click(object sender, RoutedEventArgs e)
    {
        Methods start = new Methods();
        start.currentStep = new List<StepItem>();
        string dependency = dependencyTextBox.Text;
        string time = TimeTextbox.Text;
        string priority = priorityTextbox.Text;

        start.currentStep.Add(new StepItem()
        {
            stepDescription = DescriptionTextBox.Text,
            stepDependency = Convert.ToInt32(dependency),
            stepTime = Convert.ToInt32(time),
            stepType = typeTextBox.Text,
            stepPriority = Convert.ToInt32(priority)

        });

        NewRecipe n = new NewRecipe();
        n.addSteptoList(start.currentStep);
    }

And then I need some code to add the item to the listview which for now i've got

    public void addSteptoList(List<StepItem> item)
    {
        StepsListView.Items.Add(item);
    }

This code is probably a bit of a mess now as I've been messing around with it trying to get databinding to work.

1

1 Answers

1
votes

YOu are binding to the ListView to

{Binding Source currentStep}

You need to make currentStep a public property

Remove the

public void addSteptoList(List<StepItem> item)
    {
        StepsListView.Items.Add(item);
    }

If you new currentStep in the button then all items will be lost and it will only have the last item

For data context

this.datacontext = this