ConcurrentDictionary is not updating WPF list box UI.

Abirami Ohmprakash 0 Reputation points
2024-08-21T08:06:56.04+00:00

Hi Team,

I have to bind the ConcurrentDictionary value to WPF list box. It does not update the UI whenever i add\remove the items. same i have tried with normal dictionary collection. it updates UI properly whenever there is change in the dictionary. I tried to implement INotifyPropertyChanged into that concurrentdictionary. But still I am facing the same issue. UI is not updating. Could you please let me know whether it can be possible in any other way or no option in that.

Developer technologies | C#
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2024-08-21T09:45:30.09+00:00

    Hi,@Abirami Ohmprakash. Welcome to Microsoft Q&A. 

    ConcurrentDictionary implements IEnumerable but does not implement INotifyCollectionChanged.

    If you need the collection data to be able to implement UI notification updates when adding or deleting items, you need to implement INotifyCollectionChanged instead of INotifyPropertyChanged

     

    Related links:

    INotifyCollectionChanged Interface (System.Collections.Specialized) | Microsoft Learn


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Abirami Ohmprakash 0 Reputation points
    2024-08-21T11:25:02.7466667+00:00

    Hi

    Thanks for reply. just i tried with INotifyCollectionChanged interface. But i had no luck. Please find the below code changes i have tried and help me in this.

    1. Project_1 - With Dictionary

    This is my MainWindow.Xaml,cs

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Navigation;

    using System.Windows.Shapes;

    namespace DictionarySample

    {

    /// <summary>
    
    /// Interaction logic for MainWindow.xaml
    
    /// </summary>
    
    public partial class MainWindow : Window , INotifyPropertyChanged
    
    {
    
        private string _barocdeLength;
    
        private Dictionary<int,string> _d;
    
        private Dictionary<int,string> _selectedItems;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public String BarcodeLength
    
        {
    
            get
    
            {
    
                return _barocdeLength;
    
            }
    
            set
    
            {
    
                _barocdeLength = value;
    
                OnPropertyRaised("BarcodeLength");
    
            }
    
        }
    
        public Dictionary<int,string> Barcode
    
        {
    
            get
    
            {
    
                return _d;
    
            }
    
            set
    
            {
    
                _d = value;
    
                OnPropertyRaised("Barcode");
    
            }
    
           
    
        }
    
        public Dictionary<int, string> SelctedItems
    
        {
    
            get
    
            {
    
                return _selectedItems;
    
            }
    
            set
    
            {
    
                _selectedItems = value;
    
                OnPropertyRaised("SelctedItems");
    
            }
    
        }
    
    
    
        private void OnPropertyRaised(string propertyname)
    
        {
    
            if (PropertyChanged != null)
    
            {
    
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
    
            }
    
        }
    
        public MainWindow()
    
        {
    
            Barcode = new Dictionary<int, string>();
    
            Barcode.Add(1, "1");
    
            Barcode.Add(2, "2");
    
            Barcode.Add(3, "3");
    
            Barcode.Add(4, "4");
    
            OnPropertyRaised("Barcode");
    
       
    
            InitializeComponent();
    
            this.DataContext = this;
    
        
    
        }
    
        private void BarcodeLength_TextChanged(object sender, TextChangedEventArgs e)
    
        {
    
            Barcode = new Dictionary<int, string>();
    
             
    
            for(int i=0;i< Convert.ToInt32(BarcodeLength); i++)
    
            {
    
                Barcode.Add(i, i.ToString());   
    
            }
    
            OnPropertyRaised("Barcode");
    
        }
    
    }
    

    }

    Xaml file

    <Window x:Class="DictionarySample.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    
        xmlns:local="clr-namespace:DictionarySample"
    
        mc:Ignorable="d"
    
        Title="MainWindow" Height="450" Width="800">
    
    <Grid>
    
        <Grid.RowDefinitions>
    
            <RowDefinition Height="*"/>
    
            <RowDefinition Height="*"/>
    
        </Grid.RowDefinitions>
    
        <TextBox Grid.Row="0" Name="BarcodeLengthtxt" Text="{Binding Path=BarcodeLength,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextChanged="BarcodeLength_TextChanged"/>
    
        <ListBox Name="listbox1" Grid.Row="1" ItemsSource="{Binding Barcode}" DisplayMemberPath="Value" 
    
                 >
    
    
    
        </ListBox>
    
    </Grid>
    

    </Window>

    2. project_2 - concurrentDictionary

    MainWindow.Xaml

    <Window x:Class="ConcurrentDictionarySample.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    
        xmlns:local="clr-namespace:ConcurrentDictionarySample"
    
        mc:Ignorable="d"
    
        Title="MainWindow" Height="450" Width="800">
    
    <Grid>
    
        <Grid.RowDefinitions>
    
            <RowDefinition Height="*"/>
    
            <RowDefinition Height="*"/>
    
        </Grid.RowDefinitions>
    
        <TextBox Grid.Row="0" Name="BarcodeLengthtxt" Text="{Binding Path=BarcodeLength,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextChanged="BarcodeLength_TextChanged"/>
    
        <ListBox Name="listbox1" Grid.Row="1" ItemsSource="{Binding Barcode}" DisplayMemberPath="Value" >
    
        </ListBox>
    
    </Grid>
    

    </Window>

    Mainwindow.xaml.cs

    using System;

    using System.Collections.Concurrent;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Navigation;

    using System.Windows.Shapes;

    namespace ConcurrentDictionarySample

    {

    /// <summary>
    
    /// Interaction logic for MainWindow.xaml
    
    /// </summary>
    
    public partial class MainWindow : Window , INotifyPropertyChanged
    
    {
    
        private string _barocdeLength;
    
        private ConcurrentDictionary<int, string> _d;
    
        private ConcurrentDictionary<int, string> _selectedItems;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public String BarcodeLength
    
        {
    
            get
    
            {
    
                return _barocdeLength;
    
            }
    
            set
    
            {
    
                _barocdeLength = value;
    
                OnPropertyRaised("BarcodeLength");
    
            }
    
        }
    
        public ConcurrentDictionary<int, string> Barcode
    
        {
    
            get
    
            {
    
                return _d;
    
            }
    
            set
    
            {
    
                _d = value;
    
                OnPropertyRaised("Barcode");
    
            }
    
        }
    
        public ConcurrentDictionary<int, string> SelctedItems
    
        {
    
            get
    
            {
    
                return _selectedItems;
    
            }
    
            set
    
            {
    
                _selectedItems = value;
    
                OnPropertyRaised("SelctedItems");
    
            }
    
        }
    
        private void OnPropertyRaised(string propertyname)
    
        {
    
            if (PropertyChanged != null)
    
            {
    
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
    
            }
    
        }
    
        public MainWindow()
    
        {
    
            Barcode = new ConcurrentDictionary<int, string>();
    
            Barcode.TryAdd(1, "1");
    
            Barcode.TryAdd(2, "2");
    
            Barcode.TryAdd(3, "3");
    
            Barcode.TryAdd(4, "4");
    
            OnPropertyRaised("Barcode");
    
            InitializeComponent();
    
            this.DataContext = this;
    
        }
    
        private void BarcodeLength_TextChanged(object sender, TextChangedEventArgs e)
    
        {
    
            Barcode = new ConcurrentDictionary<int, string>();
    
            for (int i = 0; i < Convert.ToInt32(BarcodeLength); i++)
    
            {
    
                Barcode.AddOrUpdate(i, i.ToString(), (j, s) => i.ToString());
    
            }
    
            OnPropertyRaised("Barcode");
    
        }
    
    }
    

    }

    0 comments No comments

  3. Viorel 122.6K Reputation points
    2024-08-21T11:47:21.6433333+00:00

    Try another approach:

    private void BarcodeLength_TextChanged( object sender, TextChangedEventArgs e )
    {
        var bc = new ConcurrentDictionary<int, string>( );
    
        for( int i = 0; i < Convert.ToInt32( BarcodeLength ); i++ )
        {
            bc.AddOrUpdate( i, i.ToString( ), ( j, s ) => i.ToString( ) );
        }
    
        Barcode = bc;
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.