WPF Datagrid Binding to ICollection - c#

i hope you can help me, because I've an very special question.
I have an View with an Datagrid. The DataContext I generate Dynamically. This is done by resolving some Interface (lets say IMyDataContext) over MEF.
This Interface has some Properties. The interesting Property for this problem is
ICollection Data {get;}
This Property is Implemented in the Backend by an ObservableCollection of an type which is known in the class which implements the Interface.
Now the Problem is, that the DataGrid don't Generate the columns. Even if I Listen to the IPropertyChanged Event of the Specific Class and then Update the Layout. Nothing changes.
So my Question is, how can I resolve this Issue?
Or is there an better Solution for my Problem?
I also tried to make the Interface Generic so that it looks like the following:
public Interface IMyDataContext<CollectionType>
{
ObservableCollection<CollectionType> Data {get;}
}
The Implementing Class Exports then with [Export(typeof(IMyDataContext<>)] or [Export(typeof(IMyDataContext<object>)]
and the class which registers the View with the DataContext via PRISM resolves them with [ImportMany(typeof(IMyDataContext<object>)] but this don't work with an ObservableCollection.
So this is why I declared the Collection as ICollection in the Interface.
Can someone explain me why it's not generating the Columns?
The Code for Binding in the View:
<DataGrid
ItemsSource="{Binding Data, UpdateSourceTrigger= PropertyChanged}"
CanUserAddRows="False"
CanUserDeleteRows="False"
AutoGenerateColumns="True"
ClipboardCopyMode="IncludeHeader" />
// Edit:
I've checked that the Data are there. And yes they are available in the View. Other ViewElements which binds also on this Data Field works perfectly. And they are directly below the Datagrid.
Example (Pseudo Code):
<DataGrid ItemsSource="{Binding Data}" /> <!-- Dont work -->
<Label Content="No Data Available" IsVisible="{Binding Data, Convert={StaticResource CollectionIsEmptyToVisibleConverter}" /> <!-- Work -->
For Information:
I fill the collection at once in an other Thread. But to be Thread Safe I Use the Dispatcher like this:
var items = new List<MyItemType>(); // MyItemType is an Class. this class I want to Display in the Datagrid
// ... fill items
Application.Current.Dispatcher.Invoke(new Action(() => this.Data = new ObservableCollection<MyItemType>(items)));
// Edit 2:
Ok, here is a code Example:
The Interface which i Mean:
public interface IDataContextInterface: INotifyPropertyChanged
{
ICollection Data { get; }
// ... Some other Properties
}
An Example Implementation of the Interface (I have multiple Implementations):
[Export(typeof(IDataContextInterface))]
public class ExampleDataContext1 : IDataContextInterface
{
private ObservableCollection<MyExampleDatagridEntry> _data;
public ObservableCollection<object> Data
{
get
{
return this._data;
}
set
{
if (this._data == value)
{
return;
}
if (value == null)
{
this._data = null;
RaisePropertyChanged("Data");
return;
}
var values = value.OfType<MyExampleDatagridEntry>();
if (values != null)
{
this._data = new ObservableCollection<MyExampleDatagridEntry>(values);
}
else
{
this._data = null;
}
RaisePropertyChanged("Data");
}
}
// Will be called in an Background Thread
public void Update()
{
var data = new ObservableCollection<MyExampleDatagridEntry>();
// .. fill the data
Application.Current.Dispatcher.Invoke(new Action(() =>
this._data =data));
}
}
The Data for this Example Model looks like:
public class MyExampleDatagridEntry : INotifyPropertyChanged
{
private _exampleString1;
public string ExampleString1
{
get
{
return this._exampleString1;
}
set
{
if (this._exampleString1 != value)
{
this._exampleString1 = value;
RaisePropertyChanged("ExampleString1");
}
}
}
private _exampleString2;
public string ExampleString2
{
get
{
return this._exampleString2;
}
set
{
if (this._exampleString2 != value)
{
this._exampleString2 = value;
RaisePropertyChanged("ExampleString2");
}
}
}
// .. Some other Properties
}
The Module which import all of them and register an new View to the Region (I don't know if there is a better way):
[ModuleExport(typeof(MyViewModule))]
public class MyViewModule : IModule
{
private ObservableCollection<IDataContextInterface> _dataContextModels;
private IRegionManager _regionManager;
[ImportingConstructor]
public MyViewModule(IRegionManager regionManager)
{
this._regionManager = regionManager;
this.DataContextModels.CollectionChanged += (s, e) => this.UpdateViews();
}
[ImportMany(typeof(IDataContextInterface))]
public ObservableCollection<IDataContextInterface> DataContextModels
{
get
{
return this._dataContextModels ?? (this._dataContextModels = new ObservableCollection<IDataContextInterface>());
}
set
{
this._dataContextModels = value;
this.UpdateViews();
}
}
private void UpdateViews()
{
foreach (var dataContext in this.DataContextModels)
{
// Short version of it. Normally i check if its registered
var view = new MyViewForDataContext{ dataContext = dataContext };
this._regionManager.Regions["MyRegion"].Add(view);
}
}
}
And finally the View looks like this (Shorten to have it simpler):
<UserControl x:Class="MyViewForDataContext">
<!-- Some Other stuff here -->
<Grid>
<!-- This Datagrid don't generate the Columns -->
<DataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="True" />
<!-- This one works -->
<Label Content="No Data Available" Visibility={Binding Data, Converter={StaticResource EmptyListToVisiblityConverter}} />
</Grid>
</UserControl>
// Edit 3 (Solution):
Ok I finally solved it. It was an Problem by my behavior which I bound to the Datagrid. In this Behavior I tried to refresh the Datagrid, which worked fine. But not if the ItemsSource is null of the Datagrid. For this reason i had to insert an null check of the ItemsSource.

Related

Can you set the selected item in a ComboBox from a viewmodel via a binding?

Why isn't my ComboBox updating from the viewmodel? My goal was to use the ComboBox as a trigger for running a query. A user selects a query and the screen updates. Meanwhile, the ComboBox should reset itself to the "Select a filter..." item. But after the selection, it is staying on the item selected by the user.
ViewModel Code
public class FilterManagerViewModel : ViewModelBase
{
private CBFilterDefinition _noSelectionFilter = new CBFilterDefinition() { FilterName = "Select a filter...", FilterDefinitionId = -1 };
public FilterManagerViewModel()
{
PopulateFilterDefinitionList();
}
public List<CBFilterDefinition> FilterDefinitions { get; private set; }
private void PopulateFilterDefinitionList()
{
FilterDefinitions = GetFilterDefinitions(_firmId, _screenId, CBBusinessLayer.UserId).OrderBy(x => x.FilterName).ToList();
FilterDefinitions.Insert(0, new CBFilterDefinition() { FilterName = "<Ad Hoc Filter...>", FilterDefinitionId = 0 });
FilterDefinitions.Insert(0, _noSelectionFilter);
OnPropertyChanged("FilterDefinitions");
SelectedFilterDefinition = _noSelectionFilter;
}
public CBFilterDefinition SelectedFilterDefinition
{
get { return _noSelectionFilter; }
set
{
if (value != null)
{
if (value.FilterDefinitionId == 0)
{
// Do some Ad Hoc filtery stuff, here.
}
else if (value.FilterDefinitionId > 0)
{
//Do some pre-defined filtery stuff, here.
}
}
OnPropertyChanged("SelectedFilterDefinition");
}
}
}
ComboBox XAML
<ComboBox Grid.Column="1" ItemsSource="{Binding Path=FilterDefinitions}" SelectedItem="{Binding Path=SelectedFilterDefinition}"
DisplayMemberPath="FilterName" Width="300" HorizontalAlignment="Left" AlternationCount="2" HorizontalContentAlignment="Stretch">
It seem pretty simple, to me. But maybe my assumption that I should be able to set the selected item via the binding is not correct. (I did try setting the binding mode to TwoWay, but that didn't make a difference)
For good measure, I verified, through the debugger, that all object references point to the same instance of _noSelectionFilter. But there is only ever one instance of that item, so I'm not sure how it could be a reference issue, anyway. Do I have to bind to SelectedIndex? The ComboBox initializes, properly, so it seems the initial setting works.
The SelectedFilterDefinition getter is being called after the OnPropertyChanged call and it's returning the one and only instance of _noSelectionFilter.
What am I missing?

WPF - Text is not appearing in Tree View

i just started learning WPF as i am moving on from WinForm. At the moment i am having difficulties displaying bind data from class to tree view.
My tree view works perfectly if i use .Items.Add() method but when it comes to binding class data to TreeView this is what i see:
Here is the c# code:
public MainWindow()
{
InitializeComponent();
Search sc = new Search();
sc.query(null, "");
this.DataContext = sc;
}
Here is the xaml
<TreeView Width="400" Height="500" Name="TreeViewB" ItemsSource="{Binding getTreeResults}" Style="{StaticResource myTreeView}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Network}">
<TextBlock Text="{Binding getNetwork}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
Edited - 2 class added
Here is my class A
class Social_Searcher
{
List<Social_Network> networks = new List<Social_Network>();
public List<Social_Network> getTreeResults { get { return networks; } }
}
Here is my class B
class Social_Network
{
private string network_name;
private List<Keypair> data;
public Social_Network()
{
data = new List<Keypair>();
}
public struct Keypair
{
public void add(string _name, string _value)
{
name = _name;
value = _value;
}
public string name, value;
}
public string Network
{
get { return network_name; }
set { network_name = value; }
}
public void add(string name, string value)
{
if (name == "network")
{
network_name = value;
}
Keypair kp = new Keypair();
kp.add(name, value);
data.Add(kp);
}
public string getNetwork()
{
return network_name;
}
public List<Keypair> getData()
{
return data;
}
public string findKey_value(string key)
{
foreach (Keypair kp in data)
{
if (kp.name == key) return kp.value.ToString();
}
return "null";
}
}
You don't give much code, but getTreeResults and getNetwork look like methods, and your TextBlock will not know how to present them (normally, it would use the results of ToString(), but I don't know if that will work with a method.
If you want those methods, you can try it this way:
public string TreeResults { get { return sc.getTreeResuls(); }}
and then
<TreeView ... ItemsSource={Binding TreeResults} ... > ...
The same goes for getNetwork. I.e., you wrap each method in a public property.
If you don't want to do that, or can't, you can use an IValueConverter
There is clearly something going on in your UI, but it's hard to tell what exactly.
You will likely find a debugging tool such as Snoop useful, as it will allow you to click on items in your UI and see how they exist in the logical tree. You can modify their properties while the program is running to experiment and learn what you need to change in your source code.
I ran into this issue when converting a Windows Forms application to WPF. I know it sounds ridiculous, but make sure that your value is stored in the TreeViewItem's "Header" property, NOT the "Name" property. Once I did this, my list populated as expected.

Displaying a selected ComboBox Item in WPF

I'm trying to construct a ComboBox that will display a selected item. I can get the combo box to display the options easily enough, but getting the selected item to display is not working. The selected item refers to a value being pulled from a database.
I know that the set method is working because I can successfully store the user's choice. However, I cannot seem to get the ComboBox to populate the selected item when I grab it from the database.
I've been frustrated over this for a few weeks now, and I feel like the mistake is simple.
Scenario:
I have an Animal Model
public class AnimalModel
{
public string AnimalName { get; set; }
...
}
I have an AnimalViewModel for each Animal:
public class AnimalViewModel: BindableBase
{
private AnimalViewModel _animal;
public AnimalViewModel(AnimalModel animal)
{
_animal = animal;
}
public string AnimalName
{
get { return _animal.Name; }
set
{
if (value != this._animal.Name)
{
this._animal.Name = value;
OnPropertyChanged("AnimalName");
}
}
}
...
}
I have an ObservableCollection of AnimalViewModel objects:
public class TemplateViewModel : BindableBase
{
private ObservableCollection<AnimalViewModel> _animals;
public TemplateViewModel(...)
{
_animal = methodReturnsAnObservableCollectionOfAnimalViewModels();
}
public ObservableCollection<AnimalViewModel> Animals
{
get { return _animals; }
set
{
if (value != this._animals)
{
this._animals = value;
OnPropertyChanged("Animals");
}
}
}
}
With this in place, I can easily display a list of AnimalNames in the ComboBox:
<ComboBox ItemsSource="{Binding Animals}"
DisplayMemberPath="AnimalName"
IsEditable="True"
IsReadOnly="True"
Text="--- Select Animal ---"/>
I now bind to a SelectedItem
public class TemplateViewModel
{
...
private AnimalViewModel _selectedAnimal;
public TemplateViewModel(MyObject, ...)
{
...
_selectedAnimal = new AnimalViewModel(new AnimalModel() { AnimalName = MyObject.AnimalName });
}
...
public AnimalViewModel SelectedAnimal
{
get { return _selectedAnimal; }
set
{
if (value != _selectedAnimal)
{
_selectedAnimal = value;
AnimalName = value.AnimalName;
OnPropertyChanged("SelectedAnimal");
}
}
}
}
So now, I have:
<ComboBox ItemsSource="{Binding Animals}"
DisplayMemberPath="AnimalName"
SelectedItem={Binding SelectedAnimal}
SelectedValuePath="AnimalName"
IsEditable="True" IsReadOnly="True"
Text="--- Select Animal ---"/>
Unfortunately, this does not populate the ComboBox with an Animal. It just pulls up the default choice Select Animal with the options populated. It does set the item correctly.
Any help would be appreciated.
You need to access the actual reference to the animal in question.
In the codebehind you create an animal which looks like the animal in the ObservableCollection, but its not the animal by reference.
Change (after Animals is loaded)
_selectedAnimal = new AnimalViewModel(...);
to (only use the Property accessor, not the backing store variable; so the change event fires, btw)
SelectedAnimal = Animals[0];
Test app (formatting like grid row/column removed)
<Label>SelectedItem</Label>
<TextBlock Text="{Binding SelectedItem, ElementName=cbMain}"/>
<Label >SelectedValue</Label>
<TextBlock Text="{Binding SelectedValue, ElementName=cbMain}"/>
<Button Click="ChangeSelectedValue">Set Selected Value</Button>
<ComboBox Name="cbMain"
ItemsSource="{Binding Ships}"
SelectedItem="{Binding Ships[0]}"
SelectedValuePath="Name"
Text="Select a Ship"/>
Result on Load of screen:
I think this is a naming discrepancy between SelectedValuePath="AnimalName" and your property.
public string Animal //<----Should Be AnimalName!
{
get { return _animal.Name; }
set
{
if (value != this._animal.Name)
{
this._animal.Name = value;
OnPropertyChanged("AnimalName");
}
}
}
change your property to AnimalName and you're good to go.

Multibinding XamDataGrid

I am trying to use the following code example from the Infragistics site and I'd like edits in the XamDataCards to be reflected in the XamDataGrid. However, my DataSource for the XamDataGrid is an ObservableCollection<Companies> in my ViewModel. How can I also bind to the card and relay updates back to my Companies object in the ViewModel?
<igDP:XamDataGrid x:Name="dgCompanies" Theme="IGTheme" DataSource="{Binding Companies}" SelectedDataItemsScope="RecordsOnly">
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings CellClickAction="SelectCell" AllowEdit="True"/>
</igDP:XamDataGrid.FieldSettings>
</igDP:XamDataGrid>
<igDP:XamDataCards x:Name="XamDataCards1"
Grid.Row="1"
DataSource="{Binding Path=SelectedDataItems, ElementName=dgCompanies}"
Theme="IGTheme">
Edit: Added ViewModel
public class CompanyMgmtViewModel : ViewModelBase
{
private ObservableCollection<Object> _Companies = null;
public ObservableCollection<Object> Companies
{
get { return _Companies; }
set
{
if (_Companies != value)
{
_Companies = value;
RaisePropertyChanged(GetPropertyName(() => Companies));
}
}
}
public CompanyMgmtViewModel()
{
this.LoadData();
}
public void LoadData()
{
ObservableCollection<Object> records = new ObservableCollection<Object>();
var results = from res in AODB.Context.TCompanies
select res;
foreach (var item in results)
if (item != null) records.Add(item);
Companies = records;
}
}
The Model/Context code is just EF Database First generated.
You would need to bind your XamDataGrid's SelectedDataItems property to a property of type object[] ie. SelectedCompanies in your ViewModel and bind to that for your XamDataCards' datasource.
The accepted answer in this thread has a sample that shows how to do this, albeit with a ListBox instead of XamDataCards:
http://www.infragistics.com/community/forums/t/89122.aspx
Just replace that ListBox with your XamDataCards control, it works and updates the XamDataGrid. The ViewModel in the example is contained in the MainWindow code-behind, so it is MVVM like you want.
more info:
http://help.infragistics.com/Help/Doc/WPF/2014.1/CLR4.0/html/xamDataGrid_Selected_Data_Items.html
IG's SelectedDataItems is an object[] :
http://help.infragistics.com/Help/Doc/WPF/2014.1/CLR4.0/html/InfragisticsWPF4.DataPresenter.v14.1~Infragistics.Windows.DataPresenter.DataPresenterBase~SelectedDataItems.html
I couldn't have gotten to this answer without Theodosius' and Ganesh's input - so thanks to them, they both had partial answers.
I first tried to bind the SelectedDataItems of the XamDataGrid to the XamDataCards by way of a property on the ViewModel as Theodosius suggested, but that wasn't enough. Thanks to Ganesh, I implemented INotifyPropertyChanged on my model objects, by inheriting from ObservableObject in MVVMLight (how did I not know the Model needed this?).
Below are the relevant pieces of code to make it work.
I also implemented PropertyChanged.Fody as documented here; that's where the TypedViewModelBase<T> and removal of RaisePropertyChanged() comes from.
I'm also creating my Model objects by using a LINQ/Automapper .Project().To<T>() call which can be found here.
Model
public class Company : ObservableObject
{
public Company() { }
public int id { get; set; }
public string strName { get; set; }
public string strDomicileCode { get; set; }
}
ViewModel
public class CompanyMgmtViewModel : TypedViewModelBase<Company>
{
private ObservableCollection<Object> _Companies = null;
private Object[] _selectedCompany = null;
public Object[] Company
{
get { return _selectedCompany; }
set
{
if (_Company != value)
{
_selectedCompany = value;
}
}
}
public ObservableCollection<Object> Companies
{
get { return _Companies; }
set
{
if (_Companies != value)
{
_Companies = value;
}
}
}
public CompanyMgmtViewModel()
{
this.LoadData();
}
public void LoadData()
{
ObservableCollection<Object> records = new ObservableCollection<Object>();
var results = AODB.Context.TCompanies.Project().To<Company>();
foreach (var item in results)
if (item != null) records.Add(item);
Companies = records;
}
}
View
<igDP:XamDataGrid x:Name="dgCompanies"
Theme="IGTheme"
DataSource="{Binding Companies, Mode=OneWay}"
SelectedDataItemsScope="RecordsOnly"
SelectedDataItems="{Binding Company}">
...
<igDP:XamDataCards x:Name="XamDataCards1"
Grid.Row="1"
DataSource="{Binding ElementName=dgCompanies, Path=SelectedDataItems}"
Theme="IGTheme">

How to Add to an ObservableCollection<T> Item with LINQ Where() in Getter?

I recently added a Disabled property to one of my domain entities. I have a List<T> of those entities in a view model. I just changed the getter to filter the list based on a UI setting to selectively show items that are disabled.
public ObservableCollection<CustomVariableGroup> CustomVariableGroups
{
get
{
if (this.ShowDisabled) { return this._customVariableGroups; }
return new ObservableCollection<CustomVariableGroup>(this._customVariableGroups.Where(x => x.Disabled == false));
}
}
The problem is that when I now try to do this (in the same view model), the item doesn't get added to the collection.
this.CustomVariableGroups.Add(newGroup);
And I think I know why: The newGroup is being added to the new ObservableCollection<T>, not the backing field of _customVariableGroups.
If the LINQ Where() extension method was able to return ObservableCollection<T> (the type of the private backing field itself) instead of IEnumerable, I don't think I'd have this issue. As it stands now, since Where() returns IEnumerable<T>, I need to wrap that inside a new ObservableCollection<T>.
What is the right/best way for me to add a new item within the same view model? Do I need to add it directly to the private backing field? I don't want to do that because I don't want to have to remember not to use the property itself.
instead of binding to your collection directly, you could bind to a ICollectionView and set a Filter in your Disabled property. no need to alter the source collection.
EDIT:
viewmodel:
//this collection just init once - eg. in ctor, use add, remove, clear to alter
public ObservableCollection<CustomVariableGroup> CustomVariableGroups {get; private set; }
//create just once in ctor
public ICollectionView MyView {get; private set;}
//ctor
this.CustomVariableGroups = new ObservableCollection<CustomVariableGroup>();
this.MyView = CollectionViewSource.GetDefaultView(this.CustomVariableGroups);
//in your disabled property set the filter
public bool ShowDisabled
{
get { return _showDisabled; }
set {
_showDisabled = value;
if (_showDisabled)
//show just disabled
this.MyView.Filter = (item) =>
{
var myitem = (CustomVariableGroup) item;
return myitem.Disabled;
};
else
{
//show all
this.MyView.Filter = (item) => { return true; };
}
this.NotifyPropertyChanged("ShowDisabled"); }
}
xaml
<CheckBox Content="ShowDisabled" IsChecked="{Binding ShowDisabled}"/>
<ListBox ItemsSource="{Binding MyView}" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
this works in my testproject
As mentioned in the comments, the right way to solve this is in the view, not the view model. This is a view concern only. I thought I'd post how I handled it, just in case this helps someone else.
First, the getter (view model) should just return the full list every time:
public ObservableCollection<CustomVariableGroup> CustomVariableGroups
{ get { return this._customVariableGroups; } }
Then, in the view, I added the filter event to my CollectionViewSource:
<CollectionViewSource x:Key="SortedGroups" Source="{Binding CustomVariableGroups}" Filter="CollectionViewSource_Filter">
And to get the list to refresh every time I clicked the Show Disabled checkbox:
Command="{Binding RefreshVariableGroupCommand}"
Then, in the code behind, I implemented the filter:
private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
{
CustomVariableGroup customVariableGroup = e.Item as CustomVariableGroup;
if (customVariableGroup == null) { return; }
if ((bool)chkShowDisabled.IsChecked)
{
// Show everything
e.Accepted = true;
return;
}
// We are not showing disabled items, so set disabled items e.Accepted to false.
if (customVariableGroup.Disabled == true)
{
e.Accepted = false;
return;
}
e.Accepted = true;
}
The only part about this, that I don't like, is that I'm using MVVM and trying to avoid code-behind classes. But, it's better to have this than hack the view model.
I would maintain your backing List and then raise the property changed for your ObservableCollection.
public ObservableCollection<CustomVariableGroup> CustomVariableGroups
{
get
{
if (this.ShowDisabled) { return this._customVariableGroups; }
return new ObservableCollection<CustomVariableGroup> (this._customVariableGroups.Where(x => x.Disabled == false));
}
}
// adds to the backing list but raises on property to rebuild and
// return the ObservableCollection
public void AddCustomVariableGroup(CustomVariableGroup newGroup)
{
this._customVariableGroups.Add(newGroup);
OnPropertyChanged("CustomVariableGroups");
}
//Example invocation
AddCustomVariableGroup(newGroup);
I had a similar problem with a WinPhone app when showing filtered result. My solution was to bind to the "ShowDisabled" property change and on this event simply Clear() and readd stuff into the main Observable: CustomVariableGroups in this case. Something like this:
public bool ShowDisabled
{
get
{
return showDisabled;
}
set
{
if (showDisabled!= value)
{
showDisabled = value;
NotifyPropertyChanged("ShowDisabled");
}
}
}
In the constructor add this: this.PropertyChanged += new PropertyChangedEventHandler(MyViewModel_PropertyChanged);
And in the Event Handler:
void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "ShowDisabled")
{
CustomVariableGroups.Clear();
foreach (var cvg in AllVariableGroups.Where(x => !x.Disabled))
{
CustomVariableGroups.Add(cvg);
}
}
}
The component bound to the CustomVariableGroups should update accordingly.
Disclaimer: of course there is a performance impact in this and you need to decide if it is negligible or not.
I would do something like #Tallmaris, but instead of this in the MyViewModel_PropertyChanged:
CustomVariableGroups.Clear();
foreach (var cvg in CustomVariableGroups.Where(x => !x.Disabled))
{
CustomVariableGroups.Add(cvg);
}
Do this:
foreach(var cvg in CustomVariableGroups.Where( x => x.Disabled))
{
CustomVariableGroups.Remove(cvg);
}
And if that doesn't work (depends if Enumerable.Where uses iterator blocks, I think), you can do this:
foreach(var cvg in Custom VariableGroups.Where( x=> x.Disabled).ToList())
{
CustomVariableGroups.Remove(cvg);
}

Categories

Resources