ObservableCollection items not showing updates - wpf

I want a list of items that can display either an item's 'greek' or 'english' name, depending on a user toggling between the two. All of the items in the list implement INPC.
Since each item has a GreekName property and an RomanName property, the strategy I am using is to simply change the items DisplayName property. Unit tests and log output indicate that the DisplayName for each item does change and does fire INPC, but the list does not update.
The list is an ObservableCollection. I am wondering if this fails to update because the hash code doesn't change? Does that mean that the only way to replace the item in the list with a new one?
Some code below...
Cheers,
Berryl
public class MasterViewModel : ViewModelBase
{
public ObservableCollection<DetailVm> AllDetailVms
{
get { return _allDetailVms; }
}
private readonly ObservableCollection<DetailVm> _allDetailVms;
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
if (e.NewItems != null && e.NewItems.Count != 0)
foreach (DetailVm vm in e.NewItems) vm.PropertyChanged += OnGreekGodChanged;
if (e.OldItems != null && e.OldItems.Count != 0)
foreach (DetailVm vm in e.OldItems) vm.PropertyChanged -= OnGreekGodChanged;
}
private void OnGreekGodChanged(object sender, PropertyChangedEventArgs e)
{
var detailVm = (DetailVm)sender;
// if DisplayName has changed we want to refresh the view & its filter
var displayName = ExprHelper.GetPropertyName<DetailVm>(x => x.DisplayName);
if (e.PropertyName == displayName)
Log.Info("'{0} reports it's display name has changed", detailVm.DisplayName);
}
private void _flipGreekOrRomanDisplay(string newName, Func<DetailVm, string> property)
{
foreach (var detailVm in _allDetailVms)
{
Log.Info("To '{0}', before change: '{1}'", newName, detailVm.DisplayName);
detailVm.DisplayName = property(detailVm);
Log.Info("To '{0}', after change: '{1}'", newName, detailVm.DisplayName);
}
NameFilterLabelText = newName;
NotifyOfPropertyChange(() => NameFilterLabelText);
NotifyOfPropertyChange(() => UseGreekName);
NotifyOfPropertyChange(() => UseRomanName);
}
}

My idiocy - my databinding was off. The code was fine and the view updates by virtue of it's items firing INPC, as expected.

Related

How to refresh ICollectionView when new item added into it's internal ObservableCollection

Please see my code below
private ObservableCollection<Person> testList = new ObservableCollection<Person>();
public ObservableCollection<Person> TestList
{
get { return testList; }
set
{
if (testList != value)
{
testList = value;
SetProperty<ObservableCollection<Person>>(ref testList, value);
}
}
}
private ListCollectionView personListView;
public ICollectionView PersonListView
{
get
{
if (personListView == null)
{
personListView = new ListCollectionView(TestList)
{
Filter = p => FilterPerson((Person)p)
};
}
return personListView;
}
}
I want to know how to update PersonListView and refresh UI when new item is inserted into TestList, or the whole TestList is re-assigned? I've tried call Refresh method in TestList set method, but when a new item is inserted into the collection, set method will not be invoked.
Thanks
I think you can add a collection changed event to the TestList observable collection and filter the PersonListView in it.
TestList.CollectionChanged += TestList_CollectionChanged;
void TestList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
/// Filter the PersonListView
}
}

Bindings seems broken while using observable collection

I have a ViewModel with an Observable Collection Property
public ObservableCollection<GeographicArea> CurrentSensorAreasList
{
get
{
return currentSensorAreasList;
}
set
{
if (currentSensorAreasList != value)
{
currentSensorAreasList = value;
OnPropertyChanged(PROPERTY_NAME_CURRENT_SENSOR_AREAS_LIST);
}
}
}
Then in my xaml i have a binding
ItemsSource="{Binding CurrentSensorAreasList}">
This Observable Collection is updated trought a method that can be call in the viewModel constructor or when a collectionchanged handler from another list gets called.
I just clear the list and then add a fewer new items. While debugging i see all my new items updated on the list. But the UI does not get updated.
When i regenerate the viewModel and then this update method gets call in the constructor the list gets updated in the UI.
Any ideas?? I don't know if the problem comes when i call the method from a handler.....
UPDATE #1
As requested i'm going the code when I update the list
I have tested two ways to do this update
private void UpdateList1()
{
if (globalAreaManagerList != null && OperationEntity != null)
{
CurrentSensorAreasList.Clear();
CurrentSensorAreasList.AddRange(globalAreaManagerList.Where(x => x != (OperationEntity as AreaManager)).SelectMany(areaRenderer => areaRenderer.AreaList));
//AddRange is an extension method.
}
}
private void UpdateList2()
{
if (globalAreaManagerList != null && OperationEntity != null)
{
CurrentSensorAreasList = new ObservableCollection<GeographicArea>(globalAreaManagerList.Where(x => x != (OperationEntity as AreaManager)).SelectMany(areaRenderer => areaRenderer.AreaList))
}
}
Both cases works when i call it from the constructor. Then I have Other Lists where the Areas changes, and i get notified via CollectionChanged Handlers.
private void globalAreaManagerList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (AreaManager newItem in e.NewItems)
{
newItem.AreaList.CollectionChanged += AreaList_CollectionChanged;
}
}
if (e.OldItems != null)
{
foreach (AreaManager oldItem in e.OldItems)
{
oldItem.AreaList.CollectionChanged -= AreaList_CollectionChanged;
}
}
UpdateList();
}
private void AreaList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
UpdateList();
}
So when i use UpdateList1 seems to work more times but suddenly the Binding is broken and then this update does not show in the UI.
If you wish change exactly an instance of collection I recomend using DependecyProperty for that case.
Here is:
public ObservableCollection<GeographicArea> CurrentSensorAreasList
{
get { return (ObservableCollection<GeographicArea>)GetValue(CurrentSensorAreasListProperty); }
set { SetValue(CurrentSensorAreasListProperty, value); }
}
public static readonly DependencyProperty CurrentSensorAreasListProperty =
DependencyProperty.Register("CurrentSensorAreasList", typeof(ObservableCollection<GeographicArea>), typeof(ownerclass));
Where ownerclass - a name of class where you put this property.
But the better way is create only one instance of ObservaleCollection and then just change its items. I mean Add, Remove, and Clear methods.

Detecting if an ObservableCollection has been modified

I have a DataGrid nested in a DockPanel. The DockPanel serves as a data context:
DockPanel1.DataContext = GetData();
The GetData() method returns an ObservableCollection.
The ObservableCollection can be modified in the DataGrid as well as in a few textboxes nested in the DockPanel. I also navigate through the collection using a DataView.
I'd like to detect if the collection has been modified and warn a user when he/she tries to close the application without saving data.
Is there any built-in mechanism that I could use (a kind of "IsDirty" flag on the collection or on the view)? If not, I guess I will have to monitor all the controls and detect any changes manually.
Thanks,
Leszek
In order to detect changes in the collection itself, you would have to attach a CollectionChanged handler. If you also need to detect changes in the objects contained in the collection, you would have to attach PropertyChanged handlers to every object (provided that the objects implements INotifyPropertyChanged).
An implementation would basically look like this:
var collection = GetData();
collection.CollectionChanged += OnCollectionChanged;
...
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
AddPropertyChanged(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
RemovePropertyChanged(e.OldItems);
break;
case NotifyCollectionChangedAction.Replace:
case NotifyCollectionChangedAction.Reset:
RemovePropertyChanged(e.OldItems);
AddPropertyChanged(e.NewItems);
break;
}
...
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
...
}
private void AddPropertyChanged(IEnumerable items)
{
if (items != null)
{
foreach (var obj in items.OfType<INotifyPropertyChanged>())
{
obj.PropertyChanged += OnPropertyChanged;
}
}
}
private void RemovePropertyChanged(IEnumerable items)
{
if (items != null)
{
foreach (var obj in items.OfType<INotifyPropertyChanged>())
{
obj.PropertyChanged -= OnPropertyChanged;
}
}
}
To elaborate a bit on Clemens' answer above, here's the simple way to use these events (on the collection, and on the contained items) to implement an IsDirty flag such as you described:
public class DirtyCollection<T> : ObservableCollection<T> where T : INotifyPropertyChanged
{
private bool isDirty = false;
public bool IsDirty
{
get { return this.isDirty; }
}
public void Clean()
{
this.isDirty = false;
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
// We aren't concerned with how the collection changed, just that it did.
this.isDirty = true;
// But we do need to add the handlers to detect property changes on each item.
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
this.AddPropertyChanged(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
this.RemovePropertyChanged(e.OldItems);
break;
case NotifyCollectionChangedAction.Replace:
case NotifyCollectionChangedAction.Reset:
this.RemovePropertyChanged(e.OldItems);
this.AddPropertyChanged(e.NewItems);
break;
}
base.OnCollectionChanged(e);
}
private void AddPropertyChanged(IEnumerable items)
{
if (items != null)
{
foreach (var obj in items.OfType<INotifyPropertyChanged>())
{
obj.PropertyChanged += OnItemPropertyChanged;
}
}
}
private void RemovePropertyChanged(IEnumerable items)
{
if (items != null)
{
foreach (var obj in items.OfType<INotifyPropertyChanged>())
{
obj.PropertyChanged -= OnItemPropertyChanged;
}
}
}
private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// A property of a contained item has changed.
this.isDirty = true;
}
}
The code should be fairly self-explanatory.
You can, of course, remove the "where T : INotifyPropertyChanged" to allow objects that don't implement that interface to be stored in the collection, but then you won't be notified of any property changes on them, as without that interface, they can't notify you of them.
And should you want to keep track of not only that the collection is dirty but also how, some additions in OnCollectionChanged and OnItemPropertyChanged to record the information passed in the event args would do that nicely.

Check only one ToolStripMenuItem

I have a ToolStrip with multiple ToolStripDropDownButtons, each has a set of DropDownItems.
When the user clicks on an DropDownItem, the check mark is shown.
By default, multiple items can be clicked and therefore multiple check marks appear.
What I'm trying to do is when the user clicks one DropDownItem, the other already checked items should be unchecked. In other words, there should always be only one checked item in the DropDown list.
I've been dabbling with it for some time but I can't really figure out how to keep the current checked item as it is while uncheck other items.
Below is the code I have as of now.
private void subietm1ToolStripMenuItem_Click(object sender, EventArgs e)
{
UncheckOtherToolStripMenuItems(sender);
}
public void UncheckOtherToolStripMenuItems(object selectedMenuItem)
{
List<ToolStripDropDownButton> dropdownButtons = new List<ToolStripDropDownButton>();
foreach (ToolStripItem item in toolStrip1.Items)
{
if (item is ToolStripDropDownButton)
{
dropdownButtons.Add((ToolStripDropDownButton)item);
}
}
foreach (ToolStripDropDownButton btn in dropdownButtons)
{
foreach (ToolStripMenuItem d in btn.DropDownItems)
{
if (d.Checked)
d.CheckState = CheckState.Unchecked;
}
}
}
If someone could shed some light on this or tell me an easy way to go about it, I'd be grateful.
Thank you.
So easy...
Implement their method as described below:
private void subietm1ToolStripMenuItem_Click(object sender, EventArgs e)
{
UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
}
public void UncheckOtherToolStripMenuItems(ToolStripMenuItem selectedMenuItem)
{
selectedMenuItem.Checked = true;
// Select the other MenuItens from the ParentMenu(OwnerItens) and unchecked this,
// The current Linq Expression verify if the item is a real ToolStripMenuItem
// and if the item is a another ToolStripMenuItem to uncheck this.
foreach (var ltoolStripMenuItem in (from object
item in selectedMenuItem.Owner.Items
let ltoolStripMenuItem = item as ToolStripMenuItem
where ltoolStripMenuItem != null
where !item.Equals(selectedMenuItem)
select ltoolStripMenuItem))
(ltoolStripMenuItem).Checked = false;
// This line is optional, for show the mainMenu after click
selectedMenuItem.Owner.Show();
}
One detail is that you can implement the same method for all click menuItens, for this add same call for method UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender); into the Event click for each ToolstripMenuItem, see this example to the another two ToolstripMenuItens:
private void subietm2ToolStripMenuItem_Click(object sender, EventArgs e)
{
UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
}
private void subietm3ToolStripMenuItem_Click(object sender, EventArgs e)
{
UncheckOtherToolStripMenuItems((ToolStripMenuItem)sender);
}
I just set all the items in my menu with the event of item_Click so if one is clicked then it will just run the code below. Dont need an event for each button that way.
private void item_Click(object sender, EventArgs e)
{
// Set the current clicked item to item
ToolStripMenuItem item = sender as ToolStripMenuItem;
// Loop through all items in the subMenu and uncheck them but do check the clicked item
foreach (ToolStripMenuItem tempItemp in (ToolStripMenuItem)item.OwnerItem.DropDownItems)
{
if (tempItemp == item)
tempItemp.Checked = true;
else
tempItemp.Checked = false;
}
}
If you want to add several items to your list during runtime and have them connected in the way above you can run the below code.
private void subItemsMenus(ToolStripMenuItem parentItem, string[] listItems)
{
// Clear tool strip items first
parentItem.DropDownItems.Clear();
// Add items that are in the list
foreach (string subMenuItem in listItems)
{
ToolStripMenuItem item = new ToolStripMenuItem();
//Name that will appear on the menu
item.Text = subMenuItem;
//Put in the Name property whatever necessary to retrieve your data on click event
item.Name = subMenuItem;
//On-Click event
item.Click += new EventHandler(item_Click);
//Add the submenu to the parent menu
parentItem.DropDownItems.Add(item);
}
I have another way which works:
Each item is going to ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
and every item has its own tag 1, 2, 3, 4, 5.
One item is checked on start and has tag = 1.
int selecteditem = 1;
bool atwork = false;
private void dzienToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
{
if (atwork) return;
else atwork = true;
selecteditem = Convert.ToInt32(((ToolStripMenuItem)sender).Tag);
foreach (ToolStripMenuItem it in sometooltipdropdown.DropDownItems)
{
if (Convert.ToInt32(it.Tag) != selecteditem)
{
it.Checked = false;
}
}
atwork = false;
}
The easiest way is add DropDownItemClicked Event and create own method:
private void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem != null)
{
CheckSelected((ToolStripDropDownButton)sender, e.ClickedItem);
}
}
private void CheckSelected(ToolStripDropDownButton button, ToolStripItem selectedItem)
{
foreach (ToolStripMenuItem item in button.DropDownItems)
{
item.Checked = (item.Name == selectedItem.Name) ? true : false;
}
}
You could get the count by copying to an array and then use extensions.
ToolStripItem[] controls = new ToolStripItem[ToolStrip.DropDownItems.Count];
ToolStrip.DropDownItems.CopyTo(controls,0);
intcheckCount = controls.Count(c => (c as ToolStripMenuItem).Checked);
if (checkCount == 0) // must keep 1 selection
item.Checked = true;
else if (checkCount > 1) //uncheck all others
controls.Cast<ToolStripMenuItem>().Where(c => c.Checked && c.Name != item.Name)
.ToList().ForEach(s => s.Checked = false);

ObservableCollection dependency property does not update when item in collection is deleted

I have a attached property of type ObservableCollection on a control. If I add or remove items from the collection, the ui does not update. However if I replace the collection within with a new one the ViewModel the ui does update.
Can someone give me an example of what I need to do within the Dependency object so that it can handle changes within the collection?
Part of the dependency object is listed below:
public class RadCalendarBehavior : DependencyObject
{
private static void OnSpecialDaysChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var calendar = d as RadCalendar;
if (e.NewValue != null)
{
calendar.DayTemplateSelector = new SpecialDaySelector((ObservableCollection<DateTime>)e.NewValue, GetSpecialDayTemplate(d));
}
}
public static ObservableCollection<DateTime> GetSpecialDays(DependencyObject obj)
{
return (ObservableCollection<DateTime>)obj.GetValue(SpecialDaysProperty);
}
public static void SetSpecialDays(DependencyObject obj, ObservableCollection<DateTime> value)
{
obj.SetValue(SpecialDaysProperty, value);
}
public static readonly DependencyProperty SpecialDaysProperty =
DependencyProperty.RegisterAttached("SpecialDays", typeof(ObservableCollection<DateTime>), typeof(RadCalendarBehavior), new UIPropertyMetadata(null, OnSpecialDaysChanged));
}
}
I understand that I need to register that the collection has changed, but I am unsure how to do this within the dependency property
A change within the collection won't trigger the OnSpecialDaysChanged callback, because the value of the dependency property hasn't changed. If you need to react to detect changes with the collection, you need to handle the event CollectionChanged event manually:
private static void OnSpecialDaysChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var calendar = d as RadCalendar;
if (e.OldValue != null)
{
var coll = (INotifyCollectionChanged)e.OldValue;
// Unsubscribe from CollectionChanged on the old collection
coll.CollectionChanged -= SpecialDays_CollectionChanged;
}
if (e.NewValue != null)
{
var coll = (ObservableCollection<DateTime>)e.NewValue;
calendar.DayTemplateSelector = new SpecialDaySelector(coll, GetSpecialDayTemplate(d));
// Subscribe to CollectionChanged on the new collection
coll.CollectionChanged += SpecialDays_CollectionChanged;
}
}
private static void SpecialDays_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// handle CollectionChanged
}
This is just to add to the answer by Thomas. In my code I do interact with the DependencyObject's properties by creating a handler object localy like below:
private static void OnSpecialDaysChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var action = new NotifyCollectionChangedEventHandler(
(o, args) =>
{
var calendar = d as RadCalendar;
if (calendar!= null)
{
// play with calendar's properties/methods
}
});
if (e.OldValue != null)
{
var coll = (INotifyCollectionChanged)e.OldValue;
// Unsubscribe from CollectionChanged on the old collection
coll.CollectionChanged -= action;
}
if (e.NewValue != null)
{
var coll = (ObservableCollection<DateTime>)e.NewValue;
// Subscribe to CollectionChanged on the new collection
coll.CollectionChanged += action;
}
}
Hope this is helpful to someone.
If you have a collection-type dependency property keep the following in mind:
If your property is a reference type, the default value specified in dependency property metadata is not a default value per instance; instead it is a default value that applies to all instances of the type. [...]
To correct this problem, you must reset the collection dependency property value to a unique instance, as part of the class constructor call.
(see MSDN Collection-Type Dependency Properties)
To answer Sam's question (I just ran into the same problem):
Make your CollectionChanged-handler non-static and unsubscribe/re-subscribe on instance-level.
private static void OnSpecialDaysChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var calendar = (RadCalendar)d;
if (e.OldValue != null)
{
var coll = (INotifyCollectionChanged)e.OldValue;
// Unsubscribe from CollectionChanged on the old collection of the DP-instance (!)
coll.CollectionChanged -= calendar.SpecialDays_CollectionChanged;
}
if (e.NewValue != null)
{
var coll = (ObservableCollection<DateTime>)e.NewValue;
calendar.DayTemplateSelector = new SpecialDaySelector(coll, GetSpecialDayTemplate(d));
// Subscribe to CollectionChanged on the new collection of the DP-instance (!)
coll.CollectionChanged += calendar.SpecialDays_CollectionChanged;
}
}
private void SpecialDays_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
// handle CollectionChanged on instance-level
}

Resources