How can I sort ListBox items using TextBox enterd value.
The match case ListBox Items show top of list box
Here is my code
XAML
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="67,39,0,0" TextWrapping="Wrap" Text="{Binding CustomerSort,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<ListBox Margin="67,79,185,26" Background="LightYellow"
DisplayMemberPath="Name" ItemsSource="{Binding CustomerColloction}"/>
</Grid>
C# code
public class Customers
{
public String Name { get; set; }
public Int32 Age { get; set; }
}
public class ViewModel
{
public ViewModel()
{
CustomerColloction = new ObservableCollection<Customers>();
CustomerColloction.Add(new Customers() { Name = "AAA", Age = 25 });
CustomerColloction.Add(new Customers() { Name = "AA", Age = 25 });
CustomerColloction.Add(new Customers() { Name = "BBB", Age = 26 });
CustomerColloction.Add(new Customers() { Name = "BB", Age = 24 });
CustomerColloction.Add(new Customers() { Name = "AAAA", Age = 13 });
CustomerColloction.Add(new Customers() { Name = "BB", Age = 11 });
CustomerColloction.Add(new Customers() { Name = "XYZ", Age = 44 });
}
public ObservableCollection<Customers> CustomerColloction { get; set; }
private String _CustomerSort = String.Empty;
public String CustomerSort
{
get
{
return _CustomerSort;
}
set
{
_CustomerSort = value;
NotifyPropertyChanged("CustomerSort");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
I want to get the result like if we enter AA in TextBox, To Show sorting item form list box
AA
AAA
AAAA
BB
BB
BBB
XYZ
Set combobox and set drop down mode
Better to sort observable collection and set to ListBox CustomerColloction.OrderBy(q=>q.Name).ToList();
I can see its a pretty vauge requirement but just to give you an idea you can manipulate your bound object collection like this to achieve similar results
Following code has been typed by a mobile device and is not verified..
CustomerColloction.Where(p=>p.name.Contains(CustomerSort)).OrderBy(q=>q.Name).ToList().Add(CustomerColloction.where(r=>r.name.Contains(CustomerSort)=false).OrderBy(s=>s.Name).toList()
And in setter of youe CustomerSort property raise notification for you bound collection
Related
In my current WPF application, it's required to generate columns of a DataGrid dynamically in my ViewModel. For that I have used the approach with System.Windows.Interactivity and behaviors.
Therefore I created ColumnsBehavior exactly like in this post:
Is it possible to get dynamic columns on wpf datagrid in mvvm pattern?
Everything worked fine, when I initialized the bounded list and generated the columns with the bindings.
When I switched to ListBox to show a DataGrid in every ListBoxItem, everything worked great again.
Now I want to add the information (model) to the bounded list at run time.
When a ListBoxItem is selected, the corresponding DataGrid should be displayed. With a Button above the DataGrid I can pass sample data to the bounded list objects. When I hit the Button, the right amount of columns with the right headers are generated, but the rows only appears the next time, in which the ListBoxItem is visible again. This means the data is appearing as recently as I refresh the grid.
To bind the Botton Command property to the ViewModel I used the ActionCommand implementation:
RelayCommand
I am using the MVVM pattern.
This is my ViewModel:
public class ViewModel
{
public ViewModel()
{
ListItems = new ObservableCollection<ListItemModel>();
ListItems.Add(new ListItemModel()
{
IsSelected = true
});
ListItems.Add(new ListItemModel()
{
IsSelected = false
});
FillCommand = new RelayCommand(FillAction);
}
public ObservableCollection<ListItemModel> ListItems { get; set; }
public ListItemModel SelectedListItem { get; set; }
public ICommand FillCommand { get; set; }
public void FillAction(object sender)
{
SelectedListItem.Entries = new ObservableCollection<Entry>()
{
new Entry()
{
Cells = new ObservableCollection<Cell>()
{
new Cell() { Cond1 = 11, Value = 99 },
new Cell() { Cond1 = 22, Value = 99 },
new Cell() { Cond1 = 33, Value = 99 }
}
},
new Entry()
{
Cells = new ObservableCollection<Cell>()
{
new Cell() { Cond1 = 11, Value = 99 },
new Cell() { Cond1 = 22, Value = 99 },
new Cell() { Cond1 = 33, Value = 99 }
},
},
new Entry()
{
Cells = new ObservableCollection<Cell>()
{
new Cell() { Cond1 = 11, Value = 99 },
new Cell() { Cond1 = 22, Value = 99 },
new Cell() { Cond1 = 33, Value = 99 }
},
}
};
SelectedListItem.GenerateGrid();
}
}
The property for holding the columns is in the ListItemModel.
The object structure behind the ItemsSource of the DataGrid and the columns property you can see in the next code:
public class ListItemModel
{
public ListItemModel()
{
Entries = new ObservableCollection<Entry>();
DataColumns = new ObservableCollection<DataGridColumn>();
}
public ObservableCollection<Entry> Entries { get; set; }
public ObservableCollection<DataGridColumn> DataColumns { get; set; }
public bool IsSelected { get; set; }
public void GenerateGrid()
{
if (Entries.Count != 0)
{
var columns = Entries[0]?.Cells;
for (int i = 0; i < columns.Count; i++)
{
Binding b = new Binding(string.Format("Cells[{0}].Value", i));
DataGridTextColumn text = new DataGridTextColumn();
text.Header = columns[i].Cond1.ToString();
text.Binding = b;
DataColumns.Add(text);
}
}
}
}
public class Entry
{
public ObservableCollection<Cell> Cells { get; set; }
public Entry() { Cells = new ObservableCollection<Cell>(); }
}
public class Cell
{
public Cell() { }
public int Cond1 { get; set; }
public int Value { get; set; }
}
}
For the view you need the namespace:
http://schemas.microsoft.com/expression/2010/interactivity
The following code sample shows the view.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding ListItems}" SelectedItem="{Binding SelectedListItem}">
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</ListBox.Resources>
</ListBox>
<DockPanel Grid.Column="1">
<Button DockPanel.Dock="Top" Command="{Binding FillCommand}">Fill Data</Button>
<DataGrid x:Name="ToleranceGrid" ColumnWidth="*" ItemsSource="{Binding Path=SelectedListItem.Entries}"
CanUserAddRows="False" SelectionMode="Single" SelectionUnit="Cell" AutoGenerateColumns="False" local:ColumnsBindingBehaviour.BindableColumns="{Binding Path=SelectedListItem.DataColumns}">
</DataGrid>
</DockPanel>
</Grid>
The ViewModel is set to the DataContext of the view in code behind.
The result should look like this:
WPF Application
The first ListItemModel is selected and I pressed the Button above the DataGrid. As you can see, the columns were generated, but the rows aren't displayed. I debugged the code, an everything is correctly set in my ViewModel. When I would select the second ListItemModel and would go back to the first one, the content will be shown correctly.
Do you have any ideas?
Your ListItemModel should implement the INotifyPropertyChanged interface and raise the PropertyChanged event when the Entries property is set to a new collection:
public class ListItemModel : INotifyPropertyChanged
{
public ListItemModel()
{
Entries = new ObservableCollection<Entry>();
DataColumns = new ObservableCollection<DataGridColumn>();
}
private ObservableCollection<Entry> _entries;
public ObservableCollection<Entry> Entries
{
get { return _entries; }
set { _entries = value; NotifyPropertyChanged(); }
}
public ObservableCollection<DataGridColumn> DataColumns { get; set; }
public bool IsSelected { get; set; }
public void GenerateGrid()
{
if (Entries.Count != 0)
{
var columns = Entries[0]?.Cells;
for (int i = 0; i < columns.Count; i++)
{
Binding b = new Binding(string.Format("Cells[{0}].Value", i));
DataGridTextColumn text = new DataGridTextColumn();
text.Header = columns[i].Cond1.ToString();
text.Binding = b;
DataColumns.Add(text);
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So, I'm at a lost, and hope for some guidance. I'm using MVVM pattern and EF.
My View contains a datagrid bound to a observable collection within the view model.
The UI has furthermore 3 comboboxes with possible data to minimize the data shown.
So I've been googling around and found nothing I could use (or might not have understood it correctly).
How do I search and select the items from a ObservableCollection, and update the datagrid accordingly?
public ObservableCollection<DimTargets> TargetParameters { get; set; }
private void SelectionChanged(object obj)
{
if (obj is FactTargets)
{
SelectedFactTarget = (FactTargets)obj;
}
else if (obj is DimLocation)
{
SelectedLocation = (DimLocation)obj;
ViewCollection.Select(x => x.Location == SelectedLocation.Location);
TargetParameters.Where(x => x.FactTargets == SelectedLocation.FactTargets);
}
else //DimBladeType
{
SelectedBladeType = (DimBladeType)obj;
int[] a = DimTargetsController.ReturnBladeType(SelectedBladeType);
foreach (int i in a)
{
TargetParameters.Single(k => k.ID == i);
}
var os = TargetParameters.Where(d => d.ID == );
TargetParameters = Convert<DimTargets>(os);
//TargetParameters.Where(ka => ka.ID == a);
//TargetParameters = new ObservableCollection<DimTargets>(TargetParameters.Where(t => t.FactTargets == SelectedBladeType.FactTargets));
//var obs = TargetParameters.Where(t => t.FactTargets == SelectedBladeType.FactTargets);
//TargetParameters = Convert<DimTargets>(obs);
//ViewCollection.Select(x => x.BladeType == SelectedBladeType.BladeType).ToList();
}
}
public ObservableCollection<T> Convert<T>(IEnumerable original)
{
return new ObservableCollection<T>(original.Cast<T>());
}
XAML:
ComboBox ItemsSource="{Binding FactTargetCollection, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedFactTarget}" DisplayMemberPath="FYQuater" Behaivor:SelectionChangedBehaviour.Command="{Binding SelectionChangedCommand}"/>
<ComboBox ItemsSource="{Binding LocationCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedLocation}" DisplayMemberPath="Location" Behaivor:SelectionChangedBehaviour.Command="{Binding SelectionChangedCommand}"/>
<ComboBox ItemsSource="{Binding BladeTypeCollection, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedBladeType}" DisplayMemberPath="BladeType" Behaivor:SelectionChangedBehaviour.Command="{Binding SelectionChangedCommand}"/>
<Button Content="Submit" Style="{StaticResource BaseButtonStyle}"></Button>
</StackPanel>
<StackPanel x:Name="WorkPanel" DockPanel.Dock="Right">
<DataGrid ItemsSource="{Binding TargetParameters}">
</DataGrid>
</StackPanel>`
Hope this gives a better idea of what i'm trying to do.
the c# code are filled with examples with what i've tried.
Cant see all of you objects but here is a quick n simple illustration...
public RelayCommand<int> FilterAgeCommand { get; set; }
public Base_ViewModel()
{
FilterAgeCommand = new RelayCommand<int>(OnFilterAgeCommand);
this.TestClassList = new ObservableCollection<TestClass>();
this.TestClassList.Add(new TestClass() { FName = "John", SName = "Doe", Age = 25 });
this.TestClassList.Add(new TestClass() { FName = "Jane", SName = "Doe", Age = 75 });
this.TestClassList.Add(new TestClass() { FName = "Mick", SName = "Doe", Age = 35 });
this.TestClassList.Add(new TestClass() { FName = "Harry", SName = "Doe", Age = 10 });
this.TestClassList.Add(new TestClass() { FName = "Linda", SName = "Doe", Age = 25 });
this.TestClassList.Add(new TestClass() { FName = "Fred", SName = "Doe", Age = 14 });
this.FilteredTestClassList = new ObservableCollection<TestClass>();
this.Age = new List<int>();
for(int i = 1; i <100; i++)
{
this.Age.Add(i);
}
}
private void OnFilterAgeCommand(int obj)
{
this.FilteredTestClassList = Convert<TestClass>((from tc in this.TestClassList where tc.Age < obj select tc).ToList());
}
public List<int> Age { get; set; }
public ObservableCollection<TestClass> TestClassList { get; set; }
private ObservableCollection<TestClass> _FilteredTestClassList;
public ObservableCollection<TestClass> FilteredTestClassList
{
get { return _FilteredTestClassList; }
set { _FilteredTestClassList = value; OnPropertyChanged("FilteredTestClassList"); }
}
public ObservableCollection<T> Convert<T>(IEnumerable original)
{
return new ObservableCollection<T>(original.Cast<T>());
}
A simple class for the demo...
public class TestClass : INotifyPropertyChanged
{
private string _FName;
public string FName
{
get { return _FName; }
set { _FName = value; NotifyPropertyChanged("FName"); }
}
private string _SName;
public string SName
{
get { return _SName; }
set { _SName = value; NotifyPropertyChanged("FName"); }
}
private int _Age;
public int Age
{
get { return _Age; }
set { _Age = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
And some XAML...
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModels="clr-namespace:_36550006.ViewModels"
xmlns:Views="clr-namespace:_36550006.Views"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"
Title="Base_View" Height="300" Width="300">
<Window.DataContext>
<ViewModels:Base_ViewModel/>
</Window.DataContext>
<Grid>
<StackPanel>
<DataGrid ItemsSource="{Binding FilteredTestClassList, NotifyOnSourceUpdated=True}"></DataGrid>
<ComboBox x:Name="cmb" ItemsSource="{Binding Age}" SelectedValuePath="{Binding Age}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding FilterAgeCommand}" CommandParameter="{Binding ElementName=cmb, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</StackPanel>
</Grid>
I essentially have two ObservableCollection, the first holds ALL of your items and the second is a filtered collection that you bind to....
I have main combobox (Categories) and depended combobox (Subcategories). I want it to display SelectedItems when window opens. All works fine in .Net 4.0, but it doesn't work in .Net 4.5. I have two computeres with these .Net versions.
In .net 4.5. only main combobox displays SelectedItem, depended doesn't. How can I fix it?
I made test project to all of you who're interested, just copy and paste. I have no idea how I can make it smaller, sry. But it is simple, clear code example 100% generates the problem.
XAML:
<Window x:Class="GridTest.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Converter="clr-namespace:GridTest"
Title="TestWindow"
Height="300"
Width="300">
<Window.Resources>
<Converter:CategoryConverter x:Key="CategoryConverter"/>
</Window.Resources>
<Grid>
<DataGrid Name="_dataGrid"
CanUserAddRows="False"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ComboBox Grid.Column="0"
Name="_categories"
ItemsSource="{Binding Categories}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedCategory, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
</ComboBox>
<ComboBox Grid.Column="1"
SelectedItem="{Binding SelectedSubcategory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource CategoryConverter}">
<Binding Path="Subcategories"/>
<Binding Path="SelectedItem"
ElementName="_categories"/>
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Code:
public class CategoryConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] == null) return null;
var subcategories = values[0] as List<Subcategory>;
if (subcategories == null) return null;
var category = values[1] as Category;
if (category == null) return subcategories;
return subcategories.Where(g => g.CategoryId == category.Id);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public enum CategoryKinds
{
Car = 0,
Fruit = 1,
}
public class Category
{
public Int32 Id { get; set; }
public String Name { get; set; }
public override Boolean Equals(object obj)
{
var c = obj as Category;
if (c == null) return false;
return Id == c.Id;
}
}
public class Subcategory
{
public Int32 Id { get; set; }
public String Name { get; set; }
public Int32 CategoryId { get; set; }
public override Boolean Equals(object obj)
{
var sc = obj as Subcategory;
if (sc == null) return false;
return Id == sc.Id;
}
}
public class DataGridItem
{
public List<Category> Categories { get; set; }
public Category SelectedCategory { get; set; }
public List<Subcategory> Subcategories { get; set; }
public Subcategory SelectedSubcategory { get; set; }
public DataGridItem()
{
Categories = new List<Category>
{
new Category
{
Id = (Int32)CategoryKinds.Car, Name = "Car"
},
new Category
{
Id = (Int32)CategoryKinds.Fruit, Name = "Fruit"
}
};
Subcategories = new List<Subcategory>
{
new Subcategory
{
Id = 1,
Name = "Volvo",
CategoryId = (Int32) CategoryKinds.Car
},
new Subcategory
{
Id = 2,
Name = "Nissan",
CategoryId = (Int32) CategoryKinds.Car
},
new Subcategory
{
Id = 3,
Name = "Banana",
CategoryId = (Int32)CategoryKinds.Fruit
},
new Subcategory
{
Id = 4,
Name = "Lemon",
CategoryId = (Int32)CategoryKinds.Fruit
},
};
}
}
/// <summary>
/// Interaction logic for TestWindow.xaml
/// </summary>
public partial class TestWindow : Window
{
public List<DataGridItem> GridItems { get; set; }
public TestWindow()
{
InitializeComponent();
DataContext = this;
GridItems = new List<DataGridItem>
{
new DataGridItem
{
SelectedCategory = new Category
{
Id = (Int32)CategoryKinds.Car, Name = "Car"
},
SelectedSubcategory = new Subcategory
{
Id = 2,
Name = "Nissan",
CategoryId = (Int32) CategoryKinds.Car
}
},
new DataGridItem
{
SelectedCategory = new Category
{
Id = (Int32)CategoryKinds.Fruit, Name = "Fruit"
},
SelectedSubcategory = new Subcategory
{
Id = 4,
Name = "Lemon",
CategoryId = (Int32) CategoryKinds.Car
}
}
};
_dataGrid.ItemsSource = GridItems;
}
}
UPDATE
With approach suggested by Ilan and charly_b code will work fine.
GridItems = new List<DataGridItem>
{
new DataGridItem(),
new DataGridItem()
};
GridItems[1].SelectedCategory = GridItems[1].Categories[0];
GridItems[1].SelectedSubcategory = GridItems[1].Subcategories[1];
GridItems[0].SelectedCategory = GridItems[0].Categories[1];
GridItems[0].SelectedSubcategory = GridItems[0].Subcategories[3];
This code will result to:
Fruit - Lemon
Car - Nissan
But I have solution that will work even if you set SelectedItem that don't belong to ItemsSource of Combobox. You can override GetHashCode method like this:
public override int GetHashCode()
{
return Name.GetHashCode();
}
Obviously, in .Net 4.5 some of WPF methods operating with searching SelectedItem in Combobox's ItemsSource have different implementation from .Net 4.0 and now they use GetHashCode method :)
Try the next changes, the best practice is to use the source collection items in order to define the selected item. Firstly it is an architectural error to use a new item to define the selection (in both 4.5 and 4 dot.net versions). And second I advice you to use the mvvm approach (including INotifyPropertyChange implementation) to develop wpf related applications, and then all selection logic have to be moved to ViewModel and separated from the code behind (xaml.cs files).
public MainWindow()
{
InitializeComponent();
DataContext = this;
var f = new DataGridItem();
var firstselectedCategory = f.Categories.FirstOrDefault();
if (firstselectedCategory != null)
{
f.SelectedCategory = firstselectedCategory;
f.SelectedSubcategory =
f.Subcategories.FirstOrDefault(subcategory => subcategory.CategoryId == firstselectedCategory.Id);
}
else
{
f.SelectedCategory = null;
f.SelectedSubcategory = null;
}
var s = new DataGridItem();
var secondSelectedCategory = s.Categories.FirstOrDefault(category => !Equals(category, f.SelectedCategory));
if (secondSelectedCategory != null)
{
s.SelectedCategory = secondSelectedCategory;
s.SelectedSubcategory =
s.Subcategories.FirstOrDefault(subcategory => subcategory.CategoryId == secondSelectedCategory.Id);
}
else
{
s.SelectedCategory = null;
s.SelectedSubcategory = null;
}
GridItems = new List<DataGridItem>
{
f,s,
};
#region
//GridItems = new List<DataGridItem>
//{
// new DataGridItem
// {
// SelectedCategory = new Category
// {
// Id = (Int32) CategoryKinds.Car,
// Name = "Car"
// },
// SelectedSubcategory = new Subcategory
// {
// Id = 2,
// Name = "Nissan",
// CategoryId = (Int32) CategoryKinds.Car
// }
// },
// new DataGridItem
// {
// SelectedCategory = new Category
// {
// Id = (Int32) CategoryKinds.Fruit,
// Name = "Fruit"
// },
// SelectedSubcategory = new Subcategory
// {
// Id = 4,
// Name = "Lemon",
// CategoryId = (Int32) CategoryKinds.Fruit
// }
// }
//};
#endregion
_dataGrid.ItemsSource = GridItems;
}
The xaml code was not changed.
How it looks like:
.
I'll be glad to help if will have problems with the code.
Regards.
The Combobox SelectedItem object must be contained inside the Combobox's ItemsSource List.
In order to make your Programm work you can replace the SelectedSubCategory Property with the following code: (I would not use it like this in the production code, but it demonstrates how it works)
private Subcategory SelectedSubcategoryM;
public Subcategory SelectedSubcategory
{
get
{
return this.SelectedSubcategoryM;
}
set
{
this.SelectedSubcategoryM = (from aTest in this.Subcategories
where aTest.Id == value.Id
select aTest).Single();
}
}
I have a ListView that holds a list of names, each name comes from a binding to an object containing a "name" field, like this:
public class User {
public User() {}
public User(string name, int ID) {
this.name = name;
this.ID = ID;
}
public string name {set; get;}
public int ID {set; get;}
}
and the listView:
<ListView Margin="10" Name="lvName" DisplayMemberPath="name"/>
and in the code behind I set lvName.ItemSource to an ObservableCollection of User objects, in the collection I have Users that can have the same Name, but not the same ID.
I also have a dataGrid, with 2 columns defined(one for name and one for ID), I want to make it so that when the user selects a row from the listview(which shows names of Users), the datagrid would be populated with all the User objects(both the name and ID) that have the same name as was selected in the listview.
how can I do that?
Note: I managed to bind the name field to a textblock, by doing this:
Text="{Binding SelectedItem.name, ElementName=lvName}"
but I have no idea how to do it on a datagrid, let alone with the ID field as well as the name field.
This could be easily done using MVVM approach.
Assuming, that User class looks like this:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
you can define this view model:
public class ViewModel
{
public ViewModel()
{
Users = new ObservableCollection<User>
{
new User { Id = 1, Name = "John" },
new User { Id = 2, Name = "Mary" },
new User { Id = 3, Name = "Peter" },
new User { Id = 4, Name = "John" },
new User { Id = 5, Name = "John" },
new User { Id = 5, Name = "Peter" }
};
UsersView = new ListCollectionView(Users)
{
Filter = obj =>
{
var user = (User)obj;
return SelectedUser != null && user.Name == SelectedUser.Name && user.Id != selectedUser.Id;
}
};
}
public ObservableCollection<User> Users { get; private set; }
public ICollectionView UsersView { get; set; }
public User SelectedUser
{
get { return selectedUser; }
set
{
if (selectedUser != value)
{
selectedUser = value;
UsersView.Refresh();
}
}
}
private User selectedUser;
}
XAML:
<StackPanel>
<ListView DisplayMemberPath="Name" ItemsSource="{Binding Users}" SelectedItem="{Binding SelectedUser}"/>
<DataGrid ItemsSource="{Binding UsersView}"/>
</StackPanel>
Result:
I have a check list box from the wpf toolkit 2. I cannot get all of the selected items. I read that I am supposed to use SelectedItemsOverride to get all of my selected items but it does not seem to work. I put a break point in the setter for TestClassSelected but it is never fired. Any Ideas?
<xctk:CheckListBox Name="MyCheckList"
ItemsSource="{Binding TestClassCollection}"
DisplayMemberPath="DisplayName"
SelectedItemsOverride="{Binding TestClassSelected}" />
.
public IEnumerable<TestClass> TestClassCollection
{
get { return _testClassCollection; }
set
{
_testClassCollection = value;
OnPropertyChanged("TestClassCollection");
}
}
public List<TestClass> TestClassSelected
{
get { return _testClassSelected; }
set
{
_testClassSelected = value;
OnPropertyChanged("TestClassSelected");
}
}
.
public class TestClass
{
public String FirstName { get; set; }
public String LastName { get; set; }
public String DisplayName {
get { return string.Format("{0} {1}", FirstName, LastName); }
}
}
This is my test data I have been using.
TestClassCollection = new List<TestClass>
{
new TestClass {FirstName = "FIrstName", LastName = "LastName"},
new TestClass {FirstName = "Brad", LastName = "Holder"},
new TestClass {FirstName = "Sam", LastName = "Ryans"},
new TestClass {FirstName = "Ryan", LastName = "Thomas"},
new TestClass {FirstName = "Lee", LastName = "Rod"},
new TestClass {FirstName = "Amanda", LastName = "Gustaf"},
new TestClass {FirstName = "Chris", LastName = "Holems"},
new TestClass {FirstName = "Doug", LastName = "Schnitzel"},
new TestClass {FirstName = "Lisa", LastName = "Bull"},
new TestClass {FirstName = "Fred", LastName = "Simpson"},
new TestClass {FirstName = "Scott", LastName = "Rogers"}
};
try add Mode and UpdateSourceTrigger at binding
<xctk:CheckListBox Name="MyCheckList"
ItemsSource="{Binding TestClassCollection}"
DisplayMemberPath="DisplayName"
SelectedItemsOverride="{Binding TestClassSelected,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
update:
I'd checked example code from closed issue at Extended WPF Toolkit CodePlex site.
Try to change TestClassSelected property to ObservableCollection. (Still keep UpdateSourceTrigger described above in .xaml)
public ObservableCollection<TestClass> TestClassSelected
I faced the same problem and solved it by assigning the private field of the property
In your example
private ObservableCollection<TestClass> _testClassSelected=new ObservableCollection<TestClass>();
I combined previous answers/comments and what worked for me was to bind SelectedItemsOverride to an ObservableCollection<T>, include UpdateSourceTrigger=PropertyChanged, and attach a method to the ObservableCollection<T>'s event CollectionChanged.
The XAML:
<xctk:CheckListBox
ItemsSource="{Binding AllItems}"
DisplayMemberPath="Display"
ValueMemberPath="Value"
SelectedItemsOverride="{Binding Path=DataContext.SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
</xctk:CheckListBox>
Code in ViewModel:
public IEnumerable<Item> AllItems { get; set; }
public ObservableCollection<Item> SelectedItems { get; set; }
public ViewModel()
{
SelectedItems = new ObservableCollection<Item>();
SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;
}
private void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Handle collection changed event
}