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.
- 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");
}
}
}