XAML:
<ComboBox x:Name="cmb" HorizontalAlignment="Left"
Margin="183,39,0,0"
VerticalAlignment="Top"
Width="120"
ItemsSource="{Binding FilteredNames, Mode=OneWay}"
IsTextSearchEnabled="True"
IsEditable="True"
TextSearch.Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged}"/>
ViewModel:
public List<string> FilteredNames
{
get
{
return (names.FindAll(x => x.Contains(filter))).ToList<string>();
}
}
public string Filter
{
get
{
return this.filter;
}
set
{
this.filter = value;
NotifyPropertyChanged("FilteredNames");
}
}
public ViewModel()
{
this.names = new List<string>() { "Jerry", "Joey", "Roger", "Raymond", "Jessica", "Mario",
"Jonathan" };
this.filter = "";
}
This is what I have implemented. Please help me out how to get filtered dropdown in combobox.
Like when I input "j", I want to get all the items containing "j" in it.
You should bind string input to ComboBox's Text property:
Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged}"
Also I would suggest using CollectionView for filtering like this:
public ICollectionView FilteredNames { get; set; }
IList<string> names = new List<string>() { "Jerry", "Joey", "Roger", "Raymond", "Jessica", "Mario", "Jonathan" };
public VM()
{
FilteredNames = CollectionViewSource.GetDefaultView(names);
FilteredNames.Filter = (obj) =>
{
if (!(obj is string str))
return false;
return str.Contains(filter);
};
}
string filter = "";
public string Filter
{
get
{
return this.filter;
}
set
{
this.filter = value;
FilteredNames?.Refresh();
}
}
Related
I have a JSON, like the image below:
I have a ComboBox inside a ListView. I want to display "pairs" in the ComboBox.
XAML:
<ListView Name="ListPairOption">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:PairClass">
<StackPanel
x:Name="pilganStack">
<WebView
x:Name="option"
local:MyProperties.HtmlString="{Binding Name}"/>
</StackPanel>
<ComboBox
x:Name="pairOption"
DisplayMemberPath="NameA"
SelectedValue="{Binding ComboBoxClass, Mode=TwoWay}"
ItemsSource="{x:Bind PilihanS}"
PlaceholderText="Pilih" />
</ListView
Codes:
try
{
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonObject questionObject = jsonObject["EXAM_QUESTION"].GetObject();
ObservableCollection<PairClass> itemL = new ObservableCollection<PairClass>();
JsonArray mapArray = questionObject["map"].GetArray();
foreach (JsonValue mapValue in mapArray)
{
JsonArray mapArrayI = mapValue.GetArray();
PairClass pair = new PairClass();
foreach (JsonValue mapValueI in mapArrayI)
{
try
{
string v = mapValueI.ToString();
pair.Name = v;
}
}
}
itemL.Add(pair);
}
JsonArray pairArray = questionObject["pairs"].GetArray();
string pairString = "";
foreach (JsonValue pairValue in pairArray)
{
JsonArray pairArrayI = pairValue.GetArray();
List<ComboBoxClass> PilihanS = new List<ComboBoxClass>();
foreach (JsonValue pairValueI in pairArrayI)
{
try
{
var collection = Regex.Matches(v, "\\\"(.*?)\\\"");
foreach (var item in collection)
{
string v3 = item.ToString().Trim('"');
pairString = v3;
}
}
}
PilihanS.Add(new ComboBoxClass() { NameA = pairString });
}
ListPairOption.ItemsSource = itemL;
}
PairClass:
public class PairClass
{
public string Name { get; set; }
public ObservableCollection<ComboBoxClass> PilihanS { get; set; }
public PairClass(string name)
{
Name = name;
}
}
public class ComboBoxClass
{
public string NameA { get; set; }
public override string ToString()
{
return this.NameA;
}
}
}
From the code above, I didn't succeed in displaying it into a ComboBox in a ListView so that the ComboBox is empty, as shown below:
How to display it into a ComboBox?
From the code above, I didn't succeed in displaying it into a ComboBox in a ListView so that the ComboBox is empty, as shown below:
If you want to access the property that out of DataType, please using Binding ElementName=Control Name then access the the outside property from the parent DataContext. please note you need to set current page DataContext as this this.DataContext = this;. it could make sure you can access PilihanS where in the code behind from DataTemplate.
<ComboBox
x:Name="pairOption"
ItemsSource="{Binding DataContext.PilihanS, ElementName=ListPairOption}"
PlaceholderText="Pilih" />
code behind
public MainPage()
{
this.InitializeComponent();
this.DataContext = this;
}
public List<ComboBoxClass> PilihanS { get; set;} = new List<ComboBoxClass>();
And delete this line where in your code above List<ComboBoxClass> PilihanS = new List<ComboBoxClass>();
I'm using MVVM with Caliburn.Micro and i have a problem.
So i have 2 comboboxes on view. First is to represent a list of countries and second of citites. I want the cities list to update whenever the country in the first list is changed, with corresponding list of cities. My problem is that cities list doesn't update.
Here is my code:
public class MyViewModel : PropertyChangedBase
{
Company company = new Company();
List<string> countries = new List<string> {"USA","Germany" };
public string Name
{
get { return company.name; }
set { company.name = value;
}
}
public List<string> Countries
{
get { return countries; }
set {
company.country = ToString();
NotifyOfPropertyChange(() => Countries);
NotifyOfPropertyChange(() => Cities);
}
}
public List<string> Cities
{
get {
switch (company.country)
{
case "USA": return new List<string> { "New York", "Los Angeles" };
case "Germany": return new List<string> { "Hamburg", "Berlin" };
default: return new List<string> { "DEFAULT", "DEFAULT" };
}
}
set { company.city = value.ToString();
NotifyOfPropertyChange(() => Cities);
}
}
}
Now the cities list remain with default members ( DEFAULT, DEFAULT). The view contains only 2 comboboxes with corresponding names:
<Grid>
<ComboBox x:Name="Countries" />
<ComboBox x:Name="Cities" />
</Grid>
Some suggestions ?
Sorry for my bad english.
Bind the SelectedItem property of the country ComboBox to a source property of the view model:
<ComboBox x:Name="Countries" ItemsSource="{Binding Countries}" SelectedItem="{Binding SelectedCountry}" />
<ComboBox x:Name="Cities" ItemsSource="{Binding Cities}" />
...and raise the PropertyChanged event for the Cities property in the setter of this one:
public class MyViewModel : PropertyChangedBase
{
Company company = new Company();
List<string> countries = new List<string> { "USA", "Germany" };
public string SelectedCountry
{
get { return company.country; }
set
{
company.country = value;
NotifyOfPropertyChange(() => SelectedCountry);
NotifyOfPropertyChange(() => Cities);
}
}
public List<string> Countries
{
get { return countries; }
set
{
countries = value;
NotifyOfPropertyChange(() => Countries);
NotifyOfPropertyChange(() => Cities);
}
}
public List<string> Cities
{
get
{
switch (company.country)
{
case "USA": return new List<string> { "New York", "Los Angeles" };
case "Germany": return new List<string> { "Hamburg", "Berlin" };
default: return new List<string> { "DEFAULT", "DEFAULT" };
}
}
}
}
Please refer to the following blog post for a complete example and more information about how to implement this kind of cascading ComboBoxes: https://blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/
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 2 related tables - CarMarks and CarMarkGroups. Any CarMark has CarMarkGroupID. I have edit form for CarMarks with combobox binding to CarMarkGroups.
I mark one of CarMarkGroup as deleted record. So, this marked CarMarkGroup is not in list binding to combobox (because I must show only active CarMarkGroups). But CarMarkGroupID in selected CarMark still links to marked as deleted CarMarkGroup. And i dont need to change this link.
But I need show this record for selected CarMark even if it was marked for deletion. What is the best practice for that? Need I build list for combobox in ChangeSelection Event? Or what?
I'd propose a solution, based on ICollectionView filtering.
View models:
public class GroupViewModel
{
public string Name { get; set; }
public bool IsDeleted { get; set; }
}
public class ItemViewModel
{
public ItemViewModel(List<GroupViewModel> groups)
{
Groups = new ListCollectionView(groups)
{
Filter = g =>
{
var group = (GroupViewModel)g;
return !group.IsDeleted || (Group == group);
}
};
}
public string Name { get; set; }
public GroupViewModel Group
{
get { return group; }
set
{
if (group != value)
{
group = value;
Groups.Refresh();
}
}
}
private GroupViewModel group;
public ICollectionView Groups { get; private set; }
}
public class ListViewModel
{
private readonly List<GroupViewModel> groups;
public ListViewModel()
{
// some sample initialization
groups = new List<GroupViewModel>
{
new GroupViewModel { Name = "Friuts" },
new GroupViewModel { Name = "Animals" },
new GroupViewModel { Name = "Trees", IsDeleted = true }
};
Items = new List<ItemViewModel>
{
new ItemViewModel(groups) { Name = "Orange", Group = groups[0] },
new ItemViewModel(groups) { Name = "Apple", Group = groups[0] },
new ItemViewModel(groups) { Name = "Banana", Group = groups[0] },
new ItemViewModel(groups) { Name = "Cat", Group = groups[1] },
new ItemViewModel(groups) { Name = "Dog", Group = groups[1] },
new ItemViewModel(groups) { Name = "Bird", Group = groups[1] },
new ItemViewModel(groups) { Name = "Oak", Group = groups[2] },
new ItemViewModel(groups) { Name = "Nut-tree", Group = groups[2] },
new ItemViewModel(groups) { Name = "Pine", Group = groups[2] },
};
}
public IEnumerable<ItemViewModel> Items { get; private set; }
}
View (it's a Window content in WPF Application project):
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<DataGrid x:Name="ItemsGrid" AutoGenerateColumns="False" ItemsSource="{Binding Items}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Group" Binding="{Binding Group.Name}"/>
</DataGrid.Columns>
</DataGrid>
<ContentControl Grid.Column="1" Content="{Binding SelectedItem, ElementName=ItemsGrid}">
<ContentControl.ContentTemplate>
<DataTemplate>
<ComboBox Height="20" ItemsSource="{Binding Groups}" SelectedItem="{Binding Group}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Grid>
Code-behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ListViewModel();
}
}
So, when item is selected, ComboBox contains all non-deleted groups PLUS group of selected item, if this group is deleted.
I am not sure i understand the connections exactly but if you still need to see that CarMarkGroup in your CarMarks ComboBox, maybe add a boolean flag in your CarMarkGroup, something like ProposedForDeletion and when that changes to true, you change some color or another element in the GUI to let the user now it was proposed for deletion, or even display a message regarding this somewhere. Do not delete it from the ObservableCollection but just mark its flag to true and keep it there and do your further check according to this new flag.