0

//Hi! I need to put my data into listBox with multiple columns I saw this link stackoverflow.com... but it talks about every thing without mention the way that I can Add Items into the columns would you please just explain how to add Data Items into columns and thanks a lot.I did the following things successfully

<ListView.View>
     <GridView>
         <GridView.Columns>
              <GridViewColumn Header="1" Width="100" DisplayMemberBinding="{Binding Path=Field1}" />
              <GridViewColumn Header="2" Width="100" DisplayMemberBinding="{Binding Path=Field2}" />
              <GridViewColumn Header="3" Width="100" DisplayMemberBinding="{Binding Path=Field3}" />
         </GridView.Columns>
     </GridView>
</ListView.View>`

 public sealed class MyListBoxItem
    {
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
    }
    public sealed class MyViewModel
    {
        public ObservableCollection<MyListBoxItem> Items { get; private set; }
        public MyViewModel()
        {
            Items = new ObservableCollection<MyListBoxItem>();
            Items.Add(new MyListBoxItem { Field1 = "One", Field2 = "Two", Field3 = "Three" });
        }
    }
1
  • Thanks for Editing I was trying to make it like this but I couldn't ;) Commented Jun 18, 2012 at 15:01

1 Answer 1

0

You would need to set the DataContext property of your Window (say it is Window1) that contains the ListBox control, inside the Window1.xaml.cs class's constructor like this:

public Window1()
{
    MyViewModel vm = new MyViewModel();

    this.DataContext = vm;
}

Next step is to set the ItemsSource property of your ListBox control (inside XAML) to the Items property that you have made available inside your ViewModel class:

<ListBox ItemsSource="{Binding Path=Items}">
    <!--Other XAML-->
</ListBox>

Additionally, you should also implement the INotifyPropertyChanged interface for your MyListBoxItem class which is explained here in MSDN. This is because implementing MVVM pattern in your WPF app. requires that you implement change notifications for oneway or twoway databindings to work (see this to learn more on DataBinding).

Here is a more detailed explanation of MVVM on MSDN.

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.