Listview TwoWay Binding With ObservableCollection

11,760

Solution 1

Your Friend class needs to implement the INotifyPropertyChanged interface, so that WPF notifies when a change has been made to that property.

Solution 2

When you have a problem with XAML bindings the first thing to do is run up your program under visual studio and look in your output window for any errors or warnings that mention XAML or Bindings. If there is a problem it will likely show up there.

Your output window can be opened by selecting View->Output from the main menu (or typing Ctrl+W, O). If you're not seeing anything then select Debug in the "Show Output From" combobox.

If you're still not seeing anything then you might need to change your WPF trace level. You can do this by selecting Debug from the main menu and choose “Options and Settings…”. Expand the Debugging node and select “Output Window”. Now in the right hand pane set the “Data Binding” value in "WPF Trace Settings" to at least Warning. You should see some info in the output window next time you run the app under VS.

enter image description here

If you’re still not getting enough information you can alter the trace level (in .Net 3.5 and above) in XAML by adding the System.Diagnostics namespace to your XAML file:

 xmlns:diagnostics=”clr-namespace:System.Diagnostics;assembly=WindowsBase”

and setting the trace level in the binding of interest like this:

<ListView Name="Panel"
      Width="Auto"
      Margin="0,200,0,0"
      HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch"
      Background="{x:Null}"
      BorderThickness="0"
      ItemsSource="{Binding Path=Friends,
                            Mode=TwoWay,   
                            diagnostics:PresentationTraceSources.TraceLevel=High}"
      ScrollViewer.HorizontalScrollBarVisibility="Disabled"
      SelectionMode="Single">

Solution 3

Your Friends collection needs to be exposed as a public property, i.e.:

private readonly ObservableCollection<Friend> _friends = new ObservableCollection<Friend>();
public ObservableCollection<Friend> Friends { get { return _friends; } }
Share:
11,760
Ahmed Ghoneim
Author by

Ahmed Ghoneim

Well done is better than well said.

Updated on June 05, 2022

Comments

  • Ahmed Ghoneim
    Ahmed Ghoneim almost 2 years

    :::::::::: ObservableCollection ::::::::::

        ObservableCollection<Friend> Friends = new ObservableCollection<Friend> ( );
    

    :::::::::: InitializeFriends ::::::::::

            private void InitializeFriends ( )
            {
                JsonObject _JsonObject = Process . FacebookClient . Get ( "/me/friends" ) as JsonObject;
    
                if ( _JsonObject != null )
                {
                    JsonArray _JsonArray = _JsonObject [ "data" ] as JsonArray;
                    foreach ( JsonObject Friend in _JsonArray )
                    {
                        Friends . Add ( new Friend ( ) { Name = Friend [ "name" ] . ToString ( ) } );
                    }
                }
            }
    

    :::::::::: Friend ::::::::::

        public class Friend
        {
            public string Name { get; set; }
            public Conversation Chat { get; set; }
            public BitmapImage Image { get; set; }
        }
    

    :::::::::: ListView ::::::::::

            <ListView Name="Panel"
                  Width="Auto"
                  Margin="0,200,0,0"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  Background="{x:Null}"
                  BorderThickness="0"
                  ItemsSource="{Binding Path=Friends,
                                        Mode=TwoWay}"
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                  SelectionMode="Single">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.View>
                <GridView ColumnHeaderContainerStyle="{StaticResource hiddenStyle}">
                    <GridViewColumn Width="200" DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <Image Width="30"
                                           Height="30"
                                           Source="{Binding Path=Image}" />
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    

    List View Not Binding May Be Binding But Not Updating ?!!!
    There is something WRONG

  • Ahmed Ghoneim
    Ahmed Ghoneim almost 13 years
    change {Binding Path = Friends} to {Binding Friends} , NO WAY :(
  • Russell Troywest
    Russell Troywest almost 13 years
    Only if he's interested in seeing changes to a specific friend which doesn't seem to be the problem. The ObservableCollection will show changes to the Friends collection if it is bound properly, even if the Friend class does not implement INotifyPropertyChanged
  • blindmeis
    blindmeis almost 13 years
    did you check you datacontext? is the instance of the itemssource the same you add and remove new items?
  • Stonetip
    Stonetip over 12 years
    Thanks. Had the same problem but implementing this solved it and kept code clean.