Answered Bind ListView ItemsSource to List or Dictionary?

  • Wednesday, September 12, 2007 11:41 AM
     
     
    I have a Dictionary and a List (as in the .NET object called "List").  Is it possible to set the ItemsSource property of my ListView to either my Dictionary.Keys, Dictionary.Values, or my List's items?  Which of these things can I do and which can I not do? 

All Replies

  • Wednesday, September 12, 2007 12:05 PM
     
     Answered

    Hello, You can try someting like this:

     

    Code Snippet

    <ListBox ItemsSource="{Binding Source={StaticResource YourDataObject}}">

     <ListBox.ItemTemplate>

      <DataTemplate>

       <StackPanel Orientation="Horizontal">

        <Label Content="{Binding Key}" />

        <TextBlock xml:space="preserve"></TextBlock>

        <Label Content="{Binding Value}" />

       </StackPanel>

      </DataTemplate>

     </ListBox.ItemTemplate>

    </ListBox>

     

    You can also find more infor in this thread.

  • Wednesday, September 12, 2007 9:44 PM
     
     
    Yes but whenever something in my Dictionary changes, the list box does not update...

     

  • Thursday, September 13, 2007 1:26 AM
     
     
    Yes, Due to Dictionary or List doesn't implement any notification mechanism, so nobody can know its change. And you can try to encapsulate you data into one ObservableCollection.
  • Friday, September 14, 2007 5:35 AM
    Moderator
     
     Answered

    Hi

     

    Further more, only the classes no matter a collection class or not should implements the INotifyPropertyChanged interface to have the functionality to refresh the UI when the value of binding source is changed. The following links provide more information about this. I hope this helps.

    http://msdn2.microsoft.com/en-us/library/ms743643.aspx

    http://msdn2.microsoft.com/en-us/library/ms752347.aspx

     

    Best Regards

    Wei Zhou