5

I'm writing a program with MVVM (C#) and XAML using Caliburn.Micro library.

I was wondering how can I bind my Listview control with some containers such as List<Type>, IEnumerable<Type> or ObservableCollection< Type>?

Views\MainView.xaml

<ListView Grid.Column="1" x:Name="FileListView" ItemsSource="{Binding ListOfFile}" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="10" Background="#FFE6EEF7">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Status" Width="100" /> 
            <GridViewColumn Header="Name" Width="85" />
            <GridViewColumn Header="Size" Width="100" />
            <GridViewColumn Header="System Type" Width="100" />
            <GridViewColumn Header="Email Count" Width="100" />
            <GridViewColumn Header="Info Count" Width="100" />
        </GridView>
    </ListView.View>
</ListView>

ViewModels\MainViewModel.cs

namespace ListBox_CaliburnMicro
{
    public class MainViewModel : Screen
    {
        // ...

        public MainViewModel()
        {

        }

        public class FileItem
        {
            public string FileStatus { get; private set; }
            public string FileName { get; private set; }
            public string FileSize { get; private set; }
            public string FileType { get; private set; }
            public string FileEmailCount { get; private set; }
            public string FileInfoCount { get; private set; }

            public FileItem(string s1 = "", string s2 = "", string s3 = "", string s4 = "", string s5 = "", string s6 = "")
            {
                FileStatus = s1;
                FileName = s2;
                FileSize = s3;
                FileType = s4;
                FileEmailCount = s5;
                FileInfoCount = s6;
            }
        }
    }
}
Michael
  • 1,453
  • 3
  • 20
  • 28
mohammadmot
  • 135
  • 2
  • 12

1 Answers1

4

At first, you should write markup for your property at the XAML. Let's image that you have:

Model:

public class Person
{
    public int IdPerson { get; set; }
    public string Name { get; set; }
    public string SurName { get; set; }
}

ViewModel:

public class MainWindowViewModel : INotifyPropertyChanged
{
    public MainWindowViewModel()
    {
        FillData();
    }

    private void FillData()
    {
        persons = new ObservableCollection<Person>();
        for (int i = 0; i < 30; i++)
        {
            persons.Add(new Person() { IdPerson = i, Name = "Ben & Joseph " + i.ToString(), SurName = "Albahari" });
        }   
    }

    private ObservableCollection<Person> persons;

    public ObservableCollection<Person> Persons
    {
        get { return persons; }
        set { persons = value;                
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if(PropertyChanged!=null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Update:

XAML:

<Window x:Class="SOWpfApplication.MainWindow"
    ...the code omitted for the brevirt...
    xmlns:vm="clr-namespace:SOWpfApplication.ViewModel"
    Title="MainWindow" Height="750" Width="925" WindowStartupLocation="CenterScreen">
<Window.DataContext>
    <vm:MainWindowViewModel/>
</Window.DataContext>
    <ListView ItemsSource="{Binding Path=Persons}">
        <ListView.View>
          <GridView>
              <GridViewColumn Header="ID" Width="Auto" 
                   DisplayMemberBinding="{Binding IdPerson}" >
               </GridViewColumn>
               <GridViewColumn DisplayMemberBinding="{Binding Name}" 
                   Header="Name" Width="Auto"/>
               <GridViewColumn DisplayMemberBinding="{Binding SurName}" 
               Header="Price" Width="Auto"/>          
          </GridView>
       </ListView.View>
    </ListView>
</Window>
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • @mohammadmot feel free to ask any question – StepUp Jan 25 '16 at 11:38
  • I just did what you said. Its working but I cant see the text of rows. Any idea? – mohammadmot Jan 25 '16 at 11:58
  • @mohammadmot please see updated XAML. I've edited `GridViewColumnds` – StepUp Jan 25 '16 at 12:25
  • @mohammadmot if my reply helps to you, then you can mark my reply as an answer to simplify the future search of other people. http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – StepUp Jan 25 '16 at 12:28
  • @@StepUp, I managed to add items into ListView in Constructor but I couldn't do the same in OnButtonClick for example. Am I missing something here? – mohammadmot Jan 27 '16 at 08:12
  • @mohammadmot just call `FillData()` method at the `Command` which handles push of Button. Are you using Command? I hope you are using `Commands`. see this post http://stackoverflow.com/questions/16314828/bind-command-in-wpf-using-mvvm – StepUp Jan 27 '16 at 08:40
  • 1
    Thank you @stepup, I did with x:Name or cal:Message.Attach but none of them didn't worked, but as you said before, I used Command (with link: http://stackoverflow.com/questions/16314828/bind-command-in-wpf-using-mvvm) and it worked, thank you so much. – mohammadmot Jan 27 '16 at 12:44
  • 1
    Hi @stepUp, can you see my new question in [link](http://stackoverflow.com/questions/35174319/how-can-i-update-items-from-listview-with-caliburn-micro-in-c) and let me know your idea, i try to wrote a simple project. – mohammadmot Feb 03 '16 at 10:45