Click here to Skip to main content
15,890,557 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Passing two parameters in Command biding in WPF and MVVM Pin
indian14316-May-16 14:39
indian14316-May-16 14:39 
GeneralRe: Passing two parameters in Command biding in WPF and MVVM Pin
Pete O'Hanlon16-May-16 20:47
mvePete O'Hanlon16-May-16 20:47 
GeneralRe: Passing two parameters in Command biding in WPF and MVVM Pin
indian14316-May-16 22:08
indian14316-May-16 22:08 
GeneralRe: Passing two parameters in Command biding in WPF and MVVM Pin
Pete O'Hanlon16-May-16 22:11
mvePete O'Hanlon16-May-16 22:11 
GeneralRe: Passing two parameters in Command biding in WPF and MVVM Pin
Meshack Musundi23-May-16 3:12
professionalMeshack Musundi23-May-16 3:12 
QuestionAdd Dictionary in ObsavableCollection Pin
indian14314-May-16 9:20
indian14314-May-16 9:20 
AnswerRe: Add Dictionary in ObsavableCollection Pin
Pete O'Hanlon14-May-16 22:07
mvePete O'Hanlon14-May-16 22:07 
AnswerRe: Add Dictionary in ObsavableCollection Pin
Richard Deeming16-May-16 1:18
mveRichard Deeming16-May-16 1:18 
Try using this class from the Windows Samples for Parallel Programming with the .NET Framework[^]:
C#
//--------------------------------------------------------------------------
// 
//  Copyright (c) Microsoft Corporation.  All rights reserved. 
// 
//  File: ObservableConcurrentDictionary.cs
//
//--------------------------------------------------------------------------

using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;

namespace System.Collections.Concurrent
{
    /// <summary>
    /// Provides a thread-safe dictionary for use with data binding.
    /// </summary>
    /// <typeparam name="TKey">Specifies the type of the keys in this collection.</typeparam>
    /// <typeparam name="TValue">Specifies the type of the values in this collection.</typeparam>
    [DebuggerDisplay("Count={Count}")]
    public class ObservableConcurrentDictionary<TKey, TValue> :
        ICollection<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>,
        INotifyCollectionChanged, INotifyPropertyChanged
    {
        private readonly SynchronizationContext _context;
        private readonly ConcurrentDictionary<TKey, TValue> _dictionary;

        /// <summary>
        /// Initializes an instance of the ObservableConcurrentDictionary class.
        /// </summary>
        public ObservableConcurrentDictionary()
        {
            _context = AsyncOperationManager.SynchronizationContext;
            _dictionary = new ConcurrentDictionary<TKey, TValue>();
        }

        /// <summary>Event raised when the collection changes.</summary>
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        /// <summary>Event raised when a property on the collection changes.</summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Notifies observers of CollectionChanged or PropertyChanged of an update to the dictionary.
        /// </summary>
        private void NotifyObserversOfChange()
        {
            var collectionHandler = CollectionChanged;
            var propertyHandler = PropertyChanged;
            if (collectionHandler != null || propertyHandler != null)
            {
                _context.Post(s =>
                {
                    if (collectionHandler != null)
                    {
                        collectionHandler(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                    }
                    if (propertyHandler != null)
                    {
                        propertyHandler(this, new PropertyChangedEventArgs("Count"));
                        propertyHandler(this, new PropertyChangedEventArgs("Keys"));
                        propertyHandler(this, new PropertyChangedEventArgs("Values"));
                    }
                }, null);
            }
        }

        /// <summary>Attempts to add an item to the dictionary, notifying observers of any changes.</summary>
        /// <param name="item">The item to be added.</param>
        /// <returns>Whether the add was successful.</returns>
        private bool TryAddWithNotification(KeyValuePair<TKey, TValue> item)
        {
            return TryAddWithNotification(item.Key, item.Value);
        }

        /// <summary>Attempts to add an item to the dictionary, notifying observers of any changes.</summary>
        /// <param name="key">The key of the item to be added.</param>
        /// <param name="value">The value of the item to be added.</param>
        /// <returns>Whether the add was successful.</returns>
        private bool TryAddWithNotification(TKey key, TValue value)
        {
            bool result = _dictionary.TryAdd(key, value);
            if (result) NotifyObserversOfChange();
            return result;
        }

        /// <summary>Attempts to remove an item from the dictionary, notifying observers of any changes.</summary>
        /// <param name="key">The key of the item to be removed.</param>
        /// <param name="value">The value of the item removed.</param>
        /// <returns>Whether the removal was successful.</returns>
        private bool TryRemoveWithNotification(TKey key, out TValue value)
        {
            bool result = _dictionary.TryRemove(key, out value);
            if (result) NotifyObserversOfChange();
            return result;
        }

        /// <summary>Attempts to add or update an item in the dictionary, notifying observers of any changes.</summary>
        /// <param name="key">The key of the item to be updated.</param>
        /// <param name="value">The new value to set for the item.</param>
        /// <returns>Whether the update was successful.</returns>
        private void UpdateWithNotification(TKey key, TValue value)
        {
            _dictionary[key] = value;
            NotifyObserversOfChange();
        }

        #region ICollection<KeyValuePair<TKey,TValue>> Members
        void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
        {
            TryAddWithNotification(item);
        }

        void ICollection<KeyValuePair<TKey, TValue>>.Clear()
        {
            ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Clear();
            NotifyObserversOfChange();
        }

        bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
        {
            return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Contains(item);
        }

        void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).CopyTo(array, arrayIndex);
        }

        int ICollection<KeyValuePair<TKey, TValue>>.Count
        {
            get { return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Count; }
        }

        bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
        {
            get { return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).IsReadOnly; }
        }

        bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
        {
            TValue temp;
            return TryRemoveWithNotification(item.Key, out temp);
        }
        #endregion

        #region IEnumerable<KeyValuePair<TKey,TValue>> Members
        IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
        {
            return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator();
        }
        #endregion

        #region IDictionary<TKey,TValue> Members
        public void Add(TKey key, TValue value)
        {
            TryAddWithNotification(key, value);
        }

        public bool ContainsKey(TKey key)
        {
            return _dictionary.ContainsKey(key);
        }

        public ICollection<TKey> Keys
        {
            get { return _dictionary.Keys; }
        }

        public bool Remove(TKey key)
        {
            TValue temp;
            return TryRemoveWithNotification(key, out temp);
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            return _dictionary.TryGetValue(key, out value);
        }

        public ICollection<TValue> Values
        {
            get { return _dictionary.Values; }
        }

        public TValue this[TKey key]
        {
            get { return _dictionary[key]; }
            set { UpdateWithNotification(key, value); }
        }
        #endregion
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Add Dictionary in ObsavableCollection Pin
indian14316-May-16 6:35
indian14316-May-16 6:35 
QuestionDatagrid is not updating the values after changing the collection in the ViewModel in WPF Pin
indian14313-May-16 11:02
indian14313-May-16 11:02 
AnswerRe: Datagrid is not updating the values after changing the collection in the ViewModel in WPF Pin
Richard Deeming13-May-16 11:16
mveRichard Deeming13-May-16 11:16 
GeneralRe: Datagrid is not updating the values after changing the collection in the ViewModel in WPF Pin
indian14313-May-16 11:44
indian14313-May-16 11:44 
GeneralRe: Datagrid is not updating the values after changing the collection in the ViewModel in WPF Pin
indian14313-May-16 21:09
indian14313-May-16 21:09 
GeneralRe: Datagrid is not updating the values after changing the collection in the ViewModel in WPF Pin
Pete O'Hanlon14-May-16 3:39
mvePete O'Hanlon14-May-16 3:39 
QuestionConvert This XAML To C# Pin
Kevin Marois12-May-16 13:45
professionalKevin Marois12-May-16 13:45 
QuestionAdd a check box and delete multiple rows from DataGrid in WPF Pin
indian14312-May-16 8:46
indian14312-May-16 8:46 
AnswerRe: Add a check box and delete multiple rows from DataGrid in WPF Pin
Mycroft Holmes12-May-16 13:06
professionalMycroft Holmes12-May-16 13:06 
GeneralRe: Add a check box and delete multiple rows from DataGrid in WPF Pin
indian14312-May-16 14:12
indian14312-May-16 14:12 
QuestionUse Different Paths In Control Template Pin
Kevin Marois11-May-16 13:14
professionalKevin Marois11-May-16 13:14 
AnswerRe: Use Different Paths In Control Template Pin
Gerry Schmitz11-May-16 16:06
mveGerry Schmitz11-May-16 16:06 
QuestionHandle Title Bar Icon Click Pin
Kevin Marois11-May-16 5:56
professionalKevin Marois11-May-16 5:56 
AnswerRe: Handle Title Bar Icon Click Pin
CHill6011-May-16 22:42
mveCHill6011-May-16 22:42 
AnswerRe: Handle Title Bar Icon Click Pin
Pete O'Hanlon11-May-16 23:50
mvePete O'Hanlon11-May-16 23:50 
Questionwpf Pin
Member 113037949-May-16 7:39
Member 113037949-May-16 7:39 
AnswerRe: wpf Pin
Pete O'Hanlon9-May-16 8:34
mvePete O'Hanlon9-May-16 8:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.