locked
ListView Data-Binding problem RRS feed

  • Question

  • Hi, I have some troubles with my program.
    I have set the binding for my listview, but when I setup my app and push the button to add an item in the collection nothing is happening.

    What's wrong?

    Here comes my C# code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Forms;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.IO;
    using System.Collections.ObjectModel;
    
    
    namespace Morse_Mail
    {
        /// <summary>
        /// Iteration logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
    
            public ObservableCollection<Message> Messages { get; set; }
    
    
    
            public MainWindow()
            {
                this.Messages = new ObservableCollection<Message>();
    
                InitializeComponent();
    
                DataContext = Messages;
            }
    
    
    
            private void start_Button_Click(object sender, RoutedEventArgs e)
            {
    
    // some code here
                    Message msg = new Message(ISubject, IDate, ISender, IID);
    
                    Messages.Add(msg);
    
                }
            }
    
    
    
        }
    
        public class Message : INotifyPropertyChanged
        {
            private string subject;
            private string date;
            private string sender;
            private string id;
    
            public string Subject
            {
                get { return subject; }
                set { subject = value; OnPropertyChanged("Subject"); }
            }
            public string Date
            {
                get { return date; }
                set { date = value; OnPropertyChanged("Date"); }
            }
            public string Sender
            {
                get { return sender; }
                set { sender = value; OnPropertyChanged("Sender"); }
            }
            public string ID
            {
                get { return id; }
                set { id = value; OnPropertyChanged("ID"); }
            }
    
    
            public Message(string _Subject, string _Date, string _Sender, string _ID) 
            {
                Subject = _Subject;
                Date = _Date;
                Sender = _Sender;
                ID = _ID;
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    And here comes Xaml code:

    <Window x:Class="Morse_Mail.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="864.5" Width="1203.5" ResizeMode="NoResize" Title="Morse Mail" WindowStartupLocation="CenterScreen">
        <Grid Height="830" VerticalAlignment="Top">
            <ListView x:Name="ListView" HorizontalAlignment="Left" Height="704" Margin="47,39,0,0" VerticalAlignment="Top" Width="600" 
                      IsSynchronizedWithCurrentItem="True" 
                      ItemsSource="{Binding Messages}">
                <ListView.View >
                    <GridView AllowsColumnReorder="true">
                        <GridViewColumn Width="300" Header="Subject" 
                                        DisplayMemberBinding="{Binding Subject}"/>
                        <GridViewColumn Width="150" Header="Date" 
                                        DisplayMemberBinding="{Binding Date}"/>
                        <GridViewColumn Width="120" Header="Sender" 
                                        DisplayMemberBinding="{Binding Sender}"/>
                        <GridViewColumn Width="20" Header="ID" 
                                        DisplayMemberBinding="{Binding ID}"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Window>

    Help me, please!




    • Edited by arcadiaq Friday, June 8, 2012 1:56 PM
    Friday, June 8, 2012 1:46 PM

Answers

  • Your window DataContext is set to Messages, then in your LIstView your binding the ItemsSource to the Path Messages.  ItemsSource should just be {Binding } without a path since it needs to use the DataContext directly.

       <ListView x:Name="ListView" HorizontalAlignment="Left" Height="704" Margin="47,39,0,0" VerticalAlignment="Top" Width="600" 
                      IsSynchronizedWithCurrentItem="True" 
                      ItemsSource="{Binding }">
                <ListView.View >
                    <GridView AllowsColumnReorder="true">
                        <GridViewColumn Width="300" Header="Subject" 
                                        DisplayMemberBinding="{Binding Subject}"/>
                        <GridViewColumn Width="150" Header="Date" 
                                        DisplayMemberBinding="{Binding Date}"/>
                        <GridViewColumn Width="120" Header="Sender" 
                                        DisplayMemberBinding="{Binding Sender}"/>
                        <GridViewColumn Width="20" Header="ID" 
                                        DisplayMemberBinding="{Binding ID}"/>
                    </GridView>
                </ListView.View>
            </ListView>

    • Edited by KP_SES Friday, June 8, 2012 1:51 PM
    • Proposed as answer by KP_SES Friday, June 8, 2012 2:05 PM
    • Marked as answer by Kee Poppy Friday, June 15, 2012 7:08 AM
    Friday, June 8, 2012 1:50 PM

All replies

  • Your window DataContext is set to Messages, then in your LIstView your binding the ItemsSource to the Path Messages.  ItemsSource should just be {Binding } without a path since it needs to use the DataContext directly.

       <ListView x:Name="ListView" HorizontalAlignment="Left" Height="704" Margin="47,39,0,0" VerticalAlignment="Top" Width="600" 
                      IsSynchronizedWithCurrentItem="True" 
                      ItemsSource="{Binding }">
                <ListView.View >
                    <GridView AllowsColumnReorder="true">
                        <GridViewColumn Width="300" Header="Subject" 
                                        DisplayMemberBinding="{Binding Subject}"/>
                        <GridViewColumn Width="150" Header="Date" 
                                        DisplayMemberBinding="{Binding Date}"/>
                        <GridViewColumn Width="120" Header="Sender" 
                                        DisplayMemberBinding="{Binding Sender}"/>
                        <GridViewColumn Width="20" Header="ID" 
                                        DisplayMemberBinding="{Binding ID}"/>
                    </GridView>
                </ListView.View>
            </ListView>

    • Edited by KP_SES Friday, June 8, 2012 1:51 PM
    • Proposed as answer by KP_SES Friday, June 8, 2012 2:05 PM
    • Marked as answer by Kee Poppy Friday, June 15, 2012 7:08 AM
    Friday, June 8, 2012 1:50 PM
  • Thanks a lot! It finally works!
    Friday, June 8, 2012 1:55 PM