Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind a container to listview control in MVVM standard with Caliburn.Micro?

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;
            }
        }
    }
}
like image 632
mohammadmot Avatar asked Sep 25 '22 14:09

mohammadmot


1 Answers

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>
like image 185
StepUp Avatar answered Sep 28 '22 04:09

StepUp