Listview TwoWay Binding With ObservableCollection

混江龙づ霸主 提交于 2019-12-11 07:02:59

问题


:::::::::: 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


回答1:


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




回答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.

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">



回答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; } }



回答4:


if your datacontext is right your binding should work. btw you just need Mode=OneWay

<ListView Name="Panel" ItemsSource="{Binding Path=Friends, Mode=OneWay}" />

so you should first check your DataContext. the easiest way is to use Snoop. you can also check your visual studio output window to see binding errors




回答5:


Please add this property to the XAML Window:

DataContext="{Binding RelativeSource={RelativeSource Self}}">


来源:https://stackoverflow.com/questions/6027434/listview-twoway-binding-with-observablecollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!