1

I am tying to bind a list of custom objects as a source to listbox where my listbox has user template as

    <DataTemplate x:Key="UserTemplate1" >
      <StackPanel Orientation="Horizontal">
        <TextBlock  Name="vmname" Text="{Binding }" Width="100"/>
        <CheckBox Name="cb" IsChecked="{Binding ischeck}"/>
       </StackPanel>
    </DataTemplate>

and I used this template as datatype for listbox

      <ListBox Name="listbox3" Visibility="Hidden" Height="134" Width="156" Margin="515,82,62,104" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource UserTemplate1}"  />

I have list of objects of custom datatype

    class item{
         string name;
         bool val; 
         } 

I am not able to bind this list to listbox. i want to bind list in such a way that whenever change is made to checkbox in list, they should reflect in list of objects(item).

       listbox3.itemsource = list_items

This does not work for me

Here, I am not able to bind listbox3 to the list of objects of class checkvm.

Kaustubh_Kharche
  • 725
  • 3
  • 13
  • 34
  • Your sample code does not show you setting `listBox3.ItemsSource` to your static collection at all. Instead, I see you are looping through your collection and adding each item to `listBox3.Items` individually. These two actions are not the same. I like to blog about beginner topics for WPF, and I would highly recommend you read the links shared in [this answer](http://stackoverflow.com/a/15684569/302677) for help with learning about how WPF's binding system works, what the DataContext is, and how to design a WPF application using the MVVM design pattern. Good luck :) – Rachel Mar 25 '15 at 20:47
  • But to clarify the difference, setting the ItemsSource means "when this loads, loop through each item in the collection and create a `` for each object, and set the `ListBoxItem.DataContext` to the object itself". If bound to an `ObservableCollection`, then anytime an item is added or removed from the collection, than the ListBox will re-evaluate the items to see what items were added or removed, and update the UI accordingly. – Rachel Mar 25 '15 at 20:48

2 Answers2

1

You problem is that the changes (which are reflected in the instance of the item class) are not automatically reflected to another view / binding to an item of the list. If you want to notify the second view that a property of the item instance has changed it has to implement INotifyPropertyChanged. The easiest way is achieve that is to use a MVVM-framework. Your item class should extend the ViewModel class which implements the change notification.

example:

public class Item : ViewModelBase
{

    private string _Name;
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            if (_Name != value)
            {
                _Name = value;
                RaisePropertyChanged(() => this.Name);
            }
        }
    }

    private bool _IsSelected;
    public bool IsSelected
    {
        get
        {
            return _IsSelected;
        }
        set
        {
            if (_IsSelected != value)
            {
                _IsSelected = value;
                RaisePropertyChanged(() => this.IsSelected);
            }
        }
    }
}
fixagon
  • 5,506
  • 22
  • 26
1

You are binding your CheckBox.Checked to "ischeck", however your item class does not contain a property called ischeck.

For binding to a list of items like this :

class item{
     string name;
     bool val; 
     } 

Your DataTemplate's bindings need to look like this :

<DataTemplate x:Key="UserTemplate1" >
  <StackPanel Orientation="Horizontal">
    <TextBlock  Name="vmname" Text="{Binding name}" Width="100"/>
    <CheckBox Name="cb" IsChecked="{Binding val}"/>
   </StackPanel>
</DataTemplate>

Also note that your class should implement INotifyPropertyChanged if you want to be able to change your item in code behind and have it automatically update the UI, or if you want to do any kind of change notification.

As a side note, I hate your (lack of) naming convention. I would highly recommend googling a standard .Net naming convention and using it. :)

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • sorry for that..i will definitely work on those naming conventions. btw actually while trying above problem.. i created a static list of objects of data type "item". when i did " listbox3.itemsource = item_list " it didnt worked... i tried " foreach (item v in list_item){ilistbox3.items.add(obj);}" this also didnt worked – Kaustubh_Kharche Mar 25 '15 at 18:52
  • @user2806673 You will have to share your code to know more for me to help you with that, as there are many potential causes for this. My best guess is you set it before the list is popualated, or are setting the wrong instance of the object. Perhaps edit your answer to include the actual code you're using? – Rachel Mar 25 '15 at 19:33
  • i added my whole code to question. do just copy xaml and C# code. project should work. – Kaustubh_Kharche Mar 25 '15 at 20:32
  • here is my all original code.. please suggest required changes https://gist.github.com/kaustubh321/e17b6a7508099ba111bf – Kaustubh_Kharche Mar 28 '15 at 07:21