ObservableCollection with INotifyPropertyChanged into DatagridComboboxColumn Binding - c#

My little project looks now quite different. Now I have ObservableCollection PackagesList with data which I want to bind with whole DataGrid (via Binding Path) and ObservableCollection DestinationItememsSource where I store cases for DataGridComboboxColumn (as ItemsSourceBinding). SelectedItem property in DatagridComboboxColumn is one value from PackageInfo (and it is one of DestinationNames cases ofc). Binding on DatagridTextBoxColumns is ok.
XAML:
<Window x:Class="test01.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="400" Loaded="Window_Loaded">
<Grid>
<Border x:Name="mainBorder" Margin="20">
<DataGrid x:Name="mainDataGrid" x:Uid="mainDataGrid" AutoGenerateColumns="False"
AlternationCount="2" SelectionMode="Single" HorizontalAlignment="Stretch">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=id}"
Header="ID" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=name}"
Header="Name" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=quantity}"
Header="Quantity" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Path=price}"
Header="Price" Width="Auto" IsReadOnly="True"/>
<DataGridComboBoxColumn ItemsSource="{Binding DestinationItemsSource}"
SelectedItemBinding="{Binding Path=destination, UpdateSourceTrigger=PropertyChanged}"
Header="Destination" Width="Auto"/>
</DataGrid.Columns>
</DataGrid>
</Border>
</Grid>
</Window>
C#:
public class PackageInfo
{
public int id { get; set; }
public string name { get; set; }
public int quantity { get; set; }
public double price { get; set; }
public string destination { get; set; }
}
public class DestinationNames : INotifyPropertyChanged
{
public string name { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public DestinationNames(string value)
{
this.name = value;
}
public string DestinationName
{
get { return name; }
set
{
name = value;
OnPropertyChanged("DestinationName");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
public partial class MainWindow : Window
{
public ObservableCollection<DestinationNames> DestinationItememsSource { get; set; }
public ObservableCollection<PackageInfo> PackagesList { get; set; }
public MainWindow()
{
InitializeComponent();
}
public void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadPackages();
}
public void LoadPackages()
{
DestinationItememsSource = new ObservableCollection<DestinationNames>();
DestinationItememsSource.Add(new DestinationNames("London"));
DestinationItememsSource.Add(new DestinationNames("Plymouth"));
DestinationItememsSource.Add(new DestinationNames("Birmingham"));
DestinationItememsSource.Add(new DestinationNames("Cambridge"));
PackagesList = new ObservableCollection<PackageInfo>();
PackagesList.Add(new PackageInfo { id = 1, name = "Potato", quantity = 3, price = 2.2, destination = "London" });
PackagesList.Add(new PackageInfo { id = 2, name = "Tomato", quantity = 5, price = 3.8, destination = "Plymouth" });
PackagesList.Add(new PackageInfo { id = 3, name = "Carrot", quantity = 1, price = 5.1, destination = "London" });
PackagesList.Add(new PackageInfo { id = 4, name = "Pea", quantity = 6, price = 1.8, destination = "Plymouth" });
PackagesList.Add(new PackageInfo { id = 5, name = "Bean", quantity = 2, price = 1.5, destination = "Birmingham" });
mainDataGrid.ItemsSource = PackagesList;
}
}
How to bind this DatagridComboboxColumn properly? Should I use INotifyCollectionChanged ?? What if I want to all data in datagrid will be automatically synced with ObservableCollection ?? Please help with some example.

Have PackageInfo implement INotifyPropertyChanged.
An ObservableCollection automatically implements INotifyCollectionChanged.
You will need to add List or ObservableCollection Destinations as a property of PackageInfo
NO class DestinationNames
Just a class DestinationName

Your binding to DestinationItememsSource does not work, because it isnt part of the PackageInfo object. You also cant bind to the Windows DataContext via FindAncestor or ElementName-binding because DataGrid-Columns wont be added to the visual tree.
One solution I can think of is using the CollectionViewSource:
<Window.Resources>
<CollectionViewSource Source="{Binding RelativeSource={RelativeSource Self}, Path=DestinationItememsSource}" x:Key="destinations">
<!--here you can also add Group and SortDescriptions-->
</CollectionViewSource>
</Window.Resources>
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource ResourceKey=destinations}}".../>
I cant test it atm, because I'm still downloading VS 2012.^^
Anotherone would be to simply add a List of Destinations to your PackageInfo object (but this would cause a lot of redundancies).

Related

How to load a list into a ComboBox DataGrid and display selected value from another list?

Goal
I am aiming to achieve the following:
Load Person List to DataGrid ✔️
Load Positions List to DataGrid column into a ComboBox ✔️
Set the Person's Position value to Position's ComboBox ❌
Visual Output
Code...
Models
I have 2 models
public class Person
{
public string Name { get; set; }
public Position Position { get; set; }
}
public class Position
{
public int PositionId { get; set; }
public string PositionTitle { get; set; }
}
View Model
public class ViewModel : BaseViewModel
{
public ViewModel()
{
People = new ObservableCollection<Person>();
People.Add(new Person { Name = "Name 1", Position = new Position { PositionId = 1, PositionTitle = "Position Title 1" } });
People.Add(new Person { Name = "Name 2", Position = new Position { PositionId = 1, PositionTitle = "Position Title 1" } });
People.Add(new Person { Name = "Name 3", Position = new Position { PositionId = 2, PositionTitle = "Position Title 2" } });
Positions = new ObservableCollection<Position>();
Positions.Add(new Position { PositionId = 1, PositionTitle = "Position Title 1" });
Positions.Add(new Position { PositionId = 2, PositionTitle = "Position Title 2" });
}
private ObservableCollection<Person> people;
public ObservableCollection<Person> People
{
get { return people; }
set
{
people = value;
OnPropertyChanged();
}
}
private ObservableCollection<Position> _positions;
public ObservableCollection<Position> Positions
{
get { return _positions; }
set
{
_positions = value;
OnPropertyChanged();
}
}
}
public class Person
{
public string Name { get; set; }
public Position Position { get; set; }
}
public class Position
{
public int PositionId { get; set; }
public string PositionTitle { get; set; }
}
View
<DataGrid ItemsSource="{Binding People}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Position Title">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.Positions, RelativeSource={RelativeSource AncestorType=DataGrid}}"
DisplayMemberPath="PositionTitle"
SelectedValue="{Binding Path=Position}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Question
How do I set the SelectedItem/Index of Position to what the Person's Position is set to?
You could override the Equals method of your Position class to define that two objects with the same id should be considered equal:
public class Position
{
public int PositionId { get; set; }
public string PositionTitle { get; set; }
public override bool Equals(object obj) =>
obj is Position p && PositionId == p.PositionId;
public override int GetHashCode() => PositionId.GetHashCode();
}
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.Positions,
RelativeSource={RelativeSource AncestorType=DataGrid}}"
DisplayMemberPath="PositionTitle"
SelectedItem="{Binding Path=Position,
UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}" />
</DataTemplate>
Nope.. doesn't work
Full answer, for all inconsistencies in the code:
If the type is used for binding, then for correct operation it must either be immutable or implement the INotifyPropertyChanged interface:
namespace PeoplePosition
{
public class Position
{
public int PositionId { get; }
public string PositionTitle { get; }
public Position(int positionId, string positionTitle)
{
PositionId = positionId;
PositionTitle = positionTitle;
}
}
}
using Simplified;
namespace PeoplePosition
{
public class Person : BaseInpc // Implementation of the "INPC" interface
{
private string _name;
private Position _position;
public string Name { get => _name; set => Set(ref _name, value); }
public Position Position { get => _position; set => Set(ref _position, value); }
}
}
If you need to ensure equality of instances by value, then you need to override the Equals and GetHashCode methods (as #mm8 already wrote).
But for mutable types, this is a bad decision, which in some cases can lead to bugs.
If you need to set the values of a reference type corresponding to some collection, then you do not need to re-create instances of this type, but assign one of the elements of the collection that already contains all the valid values.
using System.Collections.ObjectModel;
namespace PeoplePosition
{
public class ViewModel
{
public ViewModel()
{
Positions.Add(new Position(1, "Position Title 1"));
Positions.Add(new Position(2, "Position Title 2"));
People.Add(new Person { Name = "Name 1", Position = Positions[0] });
People.Add(new Person { Name = "Name 2", Position = Positions[0] });
People.Add(new Person { Name = "Name 3", Position = Positions[1] });
}
public ObservableCollection<Person> People { get; }
= new ObservableCollection<Person>();
public ObservableCollection<Position> Positions { get; }
= new ObservableCollection<Position>();
}
}
Complete XAML code example demonstrating correct operation.
<Window x:Class="PeoplePosition.PeoplePositionsWindow"
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:local="clr-namespace:PeoplePosition"
mc:Ignorable="d"
Title="PeoplePositionsWindow" Height="450" Width="800">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<UniformGrid Columns="1">
<DataGrid ItemsSource="{Binding People}" AutoGenerateColumns="False" IsReadOnly="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Position Title">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.Positions,
RelativeSource={RelativeSource AncestorType=DataGrid}}"
DisplayMemberPath="PositionTitle"
SelectedItem="{Binding Path=Position,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid ItemsSource="{Binding People}" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Binding="{Binding Position.PositionId, Mode=OneWay}"
Header="PositionId"/>
<DataGridTextColumn Binding="{Binding Position.PositionTitle, Mode=OneWay}"
Header="Position Title"/>
</DataGrid.Columns>
</DataGrid>
</UniformGrid>
</Window>

How do I use MVVM with EF 6 database first?

I have a WPF project which reads Database entries. I built the model with EF 6 from the database.
Now I am a little bit confused how to apply the MVVM Pattern.
This is the model class generated from EF 6:
public partial class Folder
{
public string FolderPath { get; set; }
public string UserGroup { get; set; }
public string Permission { get; set; }
public string AccessControllType { get; set; }
public string IsInherited { get; set; }
public int FolderId { get; set; }
}
This is the MainWindow.xaml.cs:
public partial class MainWindow : Window
{
private ADAccessRightsEntities _context = new ADAccessRightsEntities();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource folderViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("folderViewSource")));
folderViewSource.Source = _context.Folders.ToList();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
this._context.Dispose();
}
}
This is my xaml code:
<Window x:Class="PermissoinViewer.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:local="clr-namespace:PermissoinViewer"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="folderViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Folder}, CreateList=True}"/>
<CollectionViewSource x:Key="userGroupViewSource" d:DesignSource="{d:DesignInstance {x:Type local:UserGroup}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource folderViewSource}">
<DataGrid x:Name="folderDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="10,10,313,209" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn x:Name="accessControllTypeColumn" Binding="{Binding AccessControllType}" Header="Access Controll Type" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="folderIdColumn" Binding="{Binding FolderId}" Header="Folder Id" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="folderPathColumn" Binding="{Binding FolderPath}" Header="Folder Path" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="isInheritedColumn" Binding="{Binding IsInherited}" Header="Is Inherited" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="permissionColumn" Binding="{Binding Permission}" Header="Permission" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="userGroupColumn" Binding="{Binding UserGroup}" Header="User Group" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
How do I include a VM class now?
Edit1:
I created a FolderVM class.
class FolderVM : AViewModel
{
private Folder folder;
public FolderVM(Folder folder)
{
this.folder = folder;
}
public string FolderPath
{
get => folder.FolderPath;
set
{
folder.FolderPath = value;
CallPropertyChanged();
}
}
public string UserGroup
{
get => folder.UserGroup;
set
{
folder.UserGroup = value;
CallPropertyChanged();
}
}
public string Permission
{
get => folder.Permission;
set
{
folder.Permission = value;
CallPropertyChanged();
}
}
public string AccessControllType
{
get => folder.AccessControllType;
set
{
folder.AccessControllType = value;
CallPropertyChanged();
}
}
public string IsInherited
{
get => folder.IsInherited;
set
{
folder.IsInherited = value;
CallPropertyChanged();
}
}
public int FolderId
{
get => folder.FolderId;
set
{
folder.FolderId = value;
CallPropertyChanged();
}
}
}
AView Model:
abstract class AViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void CallPropertyChanged([CallerMemberName] string property = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
I want to filter them that only a specific UserGroup is displayed in the view. How should I continue?

Data Binding in Data Grid with collectionviewsource

I have an ObservableCollection of class
Public class Object
{
public string Name;
public Employee Employee;
}
public class Employee
{
// few properties
}
Here is my XMAL code for CollectionViewSource:
<CollectionViewSource x:Key="cvsTasks"
Source="{Binding Reels}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Name" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
Here is my DataGrid code:
<DataGrid ItemsSource="{Binding Source={StaticResource cvsTasks}}"/>
Now CollectionViewSource having PropertyGroupDescription on Name and I want to bind my DataGrid with Employee property of CollectionViewSource .
I dont know, if i understand your question correctly.
View:
<Window.Resources>
<CollectionViewSource x:Key="cvsTasks" Source="{Binding List}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Name" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid >
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource cvsTasks}}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="EmpID" Binding="{Binding Employee.ID}"/>
<DataGridTextColumn Header="EmpDeportment" Binding="{Binding Employee.Department}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
ViewModel:
public class ViewModel {
public System.Collections.ObjectModel.ObservableCollection<Model.Root> List { get; set; }
public ViewModel() {
List = new System.Collections.ObjectModel.ObservableCollection<Model.Root> {
new Model.Root {
Name = "Peter",
Employee = new Model.Employee {
ID = 1,
Department = "IT"
}
},
new Model.Root {
Name = "Hans",
Employee = new Model.Employee {
ID = 2,
Department = "accounting"
}
},
new Model.Root {
Name = "Bilbo",
Employee = new Model.Employee {
ID = 3,
Department = "ceo"
}
}
};
}
}
Model:
public class Model {
public class Employee {
public int ID { get; set; }
public string Department { get; set; }
}
public class Root {
public string Name { get; set; }
public Employee Employee { get; set; }
}
}
And the result:

C# WPF TwoWay CheckBox Binding not updating

stackoverflow!
Starting with my code:
XAML
<DataGrid Margin="25,112,25,10" Name="datGrid" RowHeight="30"
ColumnWidth="150" BorderThickness="1"
Style="{StaticResource AzureDataGrid}" IsReadOnly="False"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Content="CREATE" Name="headerCheckBox"
FontWeight="Bold" Width="Auto"
Checked="headerCheckBox_Checked"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Username" Binding="{Binding Path=Username}"/>
<DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}"/>
<DataGridTextColumn Header="Email" Binding="{Binding Path=Email}"/>
</DataGrid.Columns>
</DataGrid>
And C#
public partial class MainWindow : Window
{
ObservableCollection<MyData> MyItems = new ObservableCollection<MyData>();
public MainWindow()
{
datGrid.ItemsSource = MyItems;
MyItems.Add(new MyData { IsChecked = false, Username = "apetecca", FirstName = "Anthony", LastName = "Petecca", Email = "apetecca#email.com"});
MyItems.Add(new MyData { IsChecked = true, Username = "jhalls", FirstName = "Jake", LastName = "Halls", Email = "jhalls#email.com" });
}
public class MyData
{
public bool IsChecked { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
private void headerCheckBox_Checked(object sender, RoutedEventArgs e)
{
foreach (var item in MyItems)
{
item.IsChecked = true;
}
}
When I click on the headerCheckBox to check it, I've watched the variables in the foreach loop to see it is changing the items from false to true but doesn't visually show that on the DataGrid. However, if I manually check or uncheck the boxes, it displays correctly when going through that loop. The loop correctly sets the values but does not change the GUI display.
Everything I have seen online indicates it has to do with TwoWay mode and UpdateSourceTrigger. Both of these are set and I can't seem to find anything else on these.
Any help would be greatly appreciated!
EDIT-
I also tried changing my class to look like this
public class MyData
{
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
OnPropertyChanged("IsChecked");
}
}
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
This didn't resolve it either.
Implement INotifyPropertyChanged and then have your properties raise event to make this happen.
Something like:
public class MyData : INotifyPropertyChanged
{
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set
{
if (isChecked != value)
{
isChecked = value;
OnPropertyChanged("IsChecked");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}

How to filter only one datagrid which has same Itemssource as another datagrid?

First datagrid is binded to ObservableCollection from my MainViewModel class. Second datagrid which i want to filter is binded like this:
ICollectionView icv;
icv = CollectionViewSource.GetDefaultView(list1);
icv.Filter = FilterTable;
dataGrid1.ItemsSource = icv;
list1 is same ObservableCollection from first datagrid. With this code my both datagrids were filtered. Is there any way to filter only second datagrid, but not first?
You can use two different view and get this filter done. refer below code.
<StackPanel>
<DataGrid ItemsSource="{Binding PersonsViews}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid ItemsSource="{Binding PersonsFileterViews}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Age" Binding="{Binding Age}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
class MainViewModel
{
private ObservableCollection<Person> perList = new ObservableCollection<Person>();
public ObservableCollection<Person> PersonList
{
get { return perList; }
set { perList = value; }
}
public ICollectionView PersonsViews { get; set; }
public ICollectionView PersonsFileterViews { get; set; }
public MainViewModel()
{
perList.Add(new Person() { Age = 1, Name = "Test1"});
perList.Add(new Person() { Age = 2, Name = "Test2" });
perList.Add(new Person() { Age = 3, Name = "Test3" });
perList.Add(new Person() { Age = 4, Name = "Test4" });
PersonsViews = new CollectionViewSource { Source = PersonList }.View;
PersonsFileterViews = new CollectionViewSource { Source = PersonList }.View;
PersonsFileterViews.Filter = new Predicate<object>(x => ((Person)x).Age > 2);
}
}
public class Person
{
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}

Categories

Resources