Last active
November 30, 2016 08:18
-
-
Save sm-abdullah/b68988dd244b22a81f8538f084897695 to your computer and use it in GitHub Desktop.
How to use Choosen Control in MVVM ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Linq; | |
namespace WpfChoosenControlDemo | |
{ | |
public class MainViewModel : ViewModelBase | |
{ | |
private ObservableCollection<Student> _items; | |
private List<Student> _selectedItems; | |
public ObservableCollection<Student> Students | |
{ | |
get | |
{ | |
return _items; | |
} | |
set | |
{ | |
_items = value; | |
NotifyPropertyChanged("Items"); | |
} | |
} | |
public List<Student> SelectedStudents | |
{ | |
get | |
{ | |
return _selectedItems; | |
} | |
set | |
{ | |
_selectedItems = value; | |
NotifyPropertyChanged("SelectedItems"); | |
} | |
} | |
public MainViewModel() | |
{ | |
//You can populate items with your db datasoruce | |
var items = new ObservableCollection<Student>(); | |
items.Add(new Student() { Id = 1, Name = "Ali" }); | |
items.Add(new Student() { Id = 2, Name = "Ammad" }); | |
items.Add(new Student() { Id = 3, Name = "Waseem" }); | |
items.Add(new Student() { Id = 4, Name = "Hakan" }); | |
items.Add(new Student() { Id = 5, Name = "Asim" }); | |
items.Add(new Student() { Id = 6, Name = "Nouman" }); | |
items.Add(new Student() { Id = 7, Name = "Kashif" }); | |
items.Add(new Student() { Id = 8, Name = "Raees" }); | |
Students = items; | |
//Here is how you can populate selected items even from Code Behinde | |
var students = new List<Student>(); | |
students.Add(Students.First()); | |
students.Add(Students.LastOrDefault()); | |
SelectedStudents = students; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfChoosenControlDemo.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:control="clr-namespace:WpfChosenControl;assembly=WpfChosenControl" | |
xmlns:local="clr-namespace:WpfChoosenControlDemo" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="401" Width="579"> | |
<Window.Resources> | |
<!-- Here we are instanciating our view model--> | |
<local:MainViewModel x:Key="viewmodel"/> | |
</Window.Resources> | |
<!-- Bind Gird DataContext with static resource viewmodel --> | |
<Grid Margin="100,160,98,160" DataContext="{StaticResource viewmodel}"> | |
<!-- Here we are Using Chosen control exactly like ListBox Control | |
See a special property DefaultMessage which will show you message when | |
there is no selected Item | |
ValueMemberPath : it should be a uniqe property like Id of student in our case it is Id | |
DisplayMemberPath: By setting this property Chosen contorl will pick that property and use to display it value | |
In our case it is Name so it will show the students Name in chips and dropdown | |
--> | |
<control:ChosenControl SelectedItems="{Binding SelectedStudents}" DefaultMessage="Please Select Items." ItemsSource="{Binding Students}" ValueMemberPath="Id" DisplayMemberPath="Name" /> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Student : INotifyPropertyChanged | |
{ | |
private int _Id; | |
public int Id | |
{ | |
get { return _Id; } | |
set | |
{ | |
_Id = value; | |
if (PropertyChanged != null) | |
{ | |
PropertyChanged(this,new PropertyChangedEventArgs("Id")); | |
} | |
} | |
} | |
private string _Name; | |
public event PropertyChangedEventHandler PropertyChanged; | |
public string Name | |
{ | |
get { return _Name; } | |
set | |
{ | |
_Name = value; | |
if (PropertyChanged != null) | |
{ | |
PropertyChanged(this, new PropertyChangedEventArgs("Name")); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment