I use RadGridView , my business is display stock market ,so high performance is a must .
When i use Regular WPF DataGrid with MVVM Design Pattern it's working very good for my , when i replace (WPF DataGrid) with (RadGridView ) the performance became very bad , your fast response is highly appreciated , See the the Following
View Model :
In Code Behind :
I Only Declare Market Object :
When i use Regular WPF DataGrid with MVVM Design Pattern it's working very good for my , when i replace (WPF DataGrid) with (RadGridView ) the performance became very bad , your fast response is highly appreciated , See the the Following
View Model :
using INtra_Day_EXplorer.Business_Logic;<
br
>using ServiceClient.HistoricalINDEXServiceClient;<
br
>using System;<
br
>using System.Collections.Generic;<
br
>using System.Collections.ObjectModel;<
br
>using System.ComponentModel;<
br
>using System.Dynamic;<
br
>using System.Linq;<
br
>using System.Text;<
br
>using System.Windows.Documents;<
br
>using System.Timers;<
br
>using System.Diagnostics;<
br
>using System.Threading;<
br
>using System.Windows.Threading;<
br
><
br
>namespace INtra_Day_EXplorer.View<
br
>{<
br
> class Market<
br
> {<
br
> #region FBD - MSeif - 29-10-2013 Properties<
br
> private MarketWatch[] data = null;<
br
> private Stopwatch stopWatch = new Stopwatch();<
br
> private ServiceWrapper serviceWrapper = ServiceWrapper.GetSingleton();<
br
> IEnumerable<
short
> IntCompanyCode { get; set; }<
br
> IEnumerable<
short
> IntSectorCode { get; set; }<
br
> List<
short
> CompaniesSectorsCodes { get; set; }<
br
> private dynamic ExpandoTemplate { get; set; }<
br
> public Boolean IsRun { get; set; }<
br
> private int index { get; set; }<
br
> #endregion<
br
><
br
> #region FBD - MSeif - 28-10-2013 - Binding Lists for (Sector/Comapny/Comapies inside Sctors)<
br
> private ObservableCollection<
ExpandoObject
> bindingList_CurrentCompanyWatch = new AsyncObservableCollection<
ExpandoObject
>();<
br
> public ObservableCollection<
ExpandoObject
> BindingList_CurrentCompanyWatch<
br
> {<
br
> get { return bindingList_CurrentCompanyWatch; }<
br
> set<
br
> {<
br
> bindingList_CurrentCompanyWatch = value;<
br
> }<
br
> }<
br
><
br
> private ObservableCollection<
ExpandoObject
> bindingList_CurrentSectorWatch = new AsyncObservableCollection<
ExpandoObject
>();<
br
> public ObservableCollection<
ExpandoObject
> BindingList_CurrentSectorWatch<
br
> {<
br
> get { return bindingList_CurrentSectorWatch; }<
br
> set<
br
> {<
br
> bindingList_CurrentSectorWatch = value;<
br
> }<
br
> }<
br
><
br
> private ObservableCollection<
ExpandoObject
> bindingList_CurrentSectorDetailsWatch = new AsyncObservableCollection<
ExpandoObject
>();<
br
> public ObservableCollection<
ExpandoObject
> BindingList_CurrentSectorDetailsWatch<
br
> {<
br
> get { return bindingList_CurrentSectorDetailsWatch; }<
br
> set { bindingList_CurrentSectorDetailsWatch = value; }<
br
> }<
br
> #endregion<
br
><
br
> /// <
summary
><
br
> /// FBD - MSeif - 28-10-2013 <
br
> /// Get List Of Codes(Sctor/Company) and start Load data to Binding Lists (Using Backgroud thread)<
br
> /// </
summary
><
br
> public Market()<
br
> {<
br
> //get companies Codes<
br
> IntCompanyCode = CompanyManager.WorkingCompanies.FindAll((CompanyInfo cinfo) => cinfo.Code <= MainData.LAST_COMPANY_CODE && cinfo.Code >= MainData.FIRST_COMPANY_CODE).Select(c => c.Code);<
br
> //get sectors Codes<
br
> IntSectorCode = CompanyManager.WorkingCompanies.FindAll((CompanyInfo cinfo) => cinfo.Code <= MainData.LAST_PDS_CODE).Select(c => c.Code);<
br
> //set all codes(sectors/comapnies) in one list<
br
> CompaniesSectorsCodes = IntCompanyCode.ToList();<
br
> for (int i = 0; i <
IntSectorCode.ToList
().Count; i++)<br> {<
br
> CompaniesSectorsCodes.Add(IntSectorCode.ToList()[i]);<
br
> }<
br
> IsRun = true;//allow load data from WCF service<
br
><
br
> // Create new instance of the thread.<
br
> Thread marketWatchThrd = new Thread(new ThreadStart(RunMarketWatch));<
br
> // Set the thread to be a background one<
br
> marketWatchThrd.IsBackground = true;<
br
> // Start the order book requests<
br
> marketWatchThrd.Start();<
br
><
br
> }<
br
><
br
> /// <
summary
><
br
> /// FBD - MSeif - 28-10-2013<
br
> /// Get Data from WCF service and bind this data to it's List(Sectors/Companies)<
br
> /// </
summary
><
br
> protected void RunMarketWatch()<
br
> {<
br
> while (IsRun)<
br
> {<
br
> try<
br
> {<
br
> //Load Data For (Sctors/Companies)<
br
> data = serviceWrapper.ServiceReference.GetCurrentMarketWatch(CompaniesSectorsCodes.ToArray());<
br
> if (data != null)<
br
> {<
br
> for (int i = 0; i <
data.Length
; i++)<br> {<
br
> // sector check<
br
> if (data[i].SymbolID <= MainData.LAST_PDS_CODE)<
br
> {<
br
> ExpandoTemplate = ToDynamic(data[i]);//Cast each object(Sector) from BackEnd to be Dynamic object(ExpandoObject)<
br
> index = BindingList_CurrentSectorWatch.Cast<
dynamic
>().Where(c => c.SymbolID == ExpandoTemplate.SymbolID).Select<
dynamic
, int>(x => BindingList_CurrentSectorWatch.IndexOf(x)).SingleOrDefault<
int
>();//Get the Index For Current Sector in the binding list<
br
><
br
> if (IntSectorCode.ToList().Count > BindingList_CurrentSectorWatch.Count)<
br
> BindingList_CurrentSectorWatch.Add(ExpandoTemplate);//add new sector to the binding list<
br
> else<
br
> {<
br
> BindingList_CurrentSectorWatch[index] = ExpandoTemplate;//Edit sector to the binding list<
br
> }<
br
> }<
br
> else//Companies check<
br
> {<
br
> ExpandoTemplate = ToDynamic(data[i]);//Cast each object(company) from BackEnd to be Dynamic object(ExpandoObject)<
br
> index = BindingList_CurrentCompanyWatch.Cast<
dynamic
>().Where(c => c.SymbolID == ExpandoTemplate.SymbolID).Select<
dynamic
, int>(x => BindingList_CurrentCompanyWatch.IndexOf(x)).SingleOrDefault<
int
>();//Get the Index For Current company in the binding list<
br
><
br
> if (IntCompanyCode.ToList().Count > BindingList_CurrentCompanyWatch.Count)<
br
> BindingList_CurrentCompanyWatch.Add(ExpandoTemplate);//add new company to the binding list<
br
> else<
br
> {<
br
> BindingList_CurrentCompanyWatch[index] = ExpandoTemplate;//edit new sector to the binding list<
br
> }<
br
> }<
br
> }<
br
> }<
br
><
br
> }<
br
> catch (Exception ex)<
br
> {<
br
> String str = ex.Message;<
br
> // throw;<
br
> }<
br
> Thread.Sleep(200);<
br
> }<
br
><
br
> }<
br
><
br
> /// <
summary
><
br
> /// FBD - MSeif - 28-10-2013<
br
> /// Cast any object to Dynamic object(ExpandoObject)<
br
> /// </
summary
><
br
> /// <
param
name
=
"value"
>Object to be Cast</
param
><
br
> /// <
returns
>Dynamic Object(Exbando Object)</
returns
><
br
> public static dynamic ToDynamic(object value)<
br
> {<
br
> IDictionary<
string
, object> expando = new ExpandoObject();<
br
><
br
> foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))<
br
> expando.Add(property.Name, property.GetValue(value));<
br
><
br
> return expando as ExpandoObject;<
br
> }<
br
><
br
> /// <
summary
><
br
> /// FBD - MSeif -29-10-2013<
br
> /// Load Childs Companies to Specific sector<
br
> /// </
summary
><
br
> /// <
param
name
=
"SectorSymbolID"
Sector Symbol ID></
param
><
br
> public void LoadChilds(short[] codes)<
br
> {<
br
> data = (dynamic)serviceWrapper.ServiceReference.GetCurrentMarketWatch(codes);<
br
> if (data != null)<
br
> {<
br
> bindingList_CurrentSectorDetailsWatch = (dynamic)(data);//new ObservableCollection<
MarketWatch
><
br
> }<
br
> }<
br
><
br
><
br
><
br
><
br
> }<
br
>}<
br
><
div
></
div
>
XAML :
<
div
><
UserControl
xmlns
=
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<br> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" <
br
> xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" <
br
> xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=QuickStart.Common" <
br
> xmlns:local="clr-namespace:INtra_Day_EXplorer.View.UserControls" <
br
> xmlns:d="http://schemas.microsoft.com/expression/blend/2008" <
br
> xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" <
br
> mc:Ignorable="d" <
br
> x:Class ="INtra_Day_EXplorer.View.UserControls.ucMarketWatch"<
br
> xmlns:MarketWatch="clr-namespace:INtra_Day_EXplorer.View" <
br
> d:DesignWidth="535" d:DesignHeight="205"><
br
> <
UserControl.Resources
><
br
> <
DataTemplate
x:Key
=
"StockChangeTemplate"
><
br
> <
local:ucStockPricePresenter
StockPrice
=
"{Binding Change}"
OldStockPrice
=
"{Binding ChangePrev}"
/><
br
> </
DataTemplate
><
br
> <
DataTemplate
x:Key
=
"StockChangePercTemplate"
><
br
> <
local:ucStockPricePresenter
StockPrice
=
"{Binding ChangePerc}"
OldStockPrice
=
"{Binding ChangePercPrev}"
/><
br
> </
DataTemplate
><
br
> <
DataTemplate
x:Key
=
"StockMoneyValueTemplate"
><
br
> <
local:ucStockPricePresenter
StockPrice
=
"{Binding MoneyValue}"
OldStockPrice
=
"{Binding MoneyValuePrev}"
/><
br
> </
DataTemplate
><
br
><
br
> <
DataTemplate
x:Key
=
"StockTradesTemplate"
><
br
> <
local:ucStockPricePresenter
StockPrice
=
"{Binding _trades}"
OldStockPrice
=
"{Binding TradesPrev}"
/><
br
> </
DataTemplate
><
br
> <
DataTemplate
x:Key
=
"StockTransactionPriceTemplate"
><
br
> <
local:ucStockPricePresenter
StockPrice
=
"{Binding _transactionPrice}"
OldStockPrice
=
"{Binding TransactionPricePrev}"
/><
br
> </
DataTemplate
><
br
> <
DataTemplate
x:Key
=
"StockVolumeTemplate"
><
br
> <
local:ucStockPricePresenter
StockPrice
=
"{Binding Volume}"
OldStockPrice
=
"{Binding VolumePrev}"
/><
br
> </
DataTemplate
><
br
><
br
> <
Style
x:Key
=
"DataGridContentCellCentering"
TargetType
=
"{x:Type DataGridCell}"
><
br
> <
Setter
Property
=
"Template"
><
br
> <
Setter.Value
><
br
> <
ControlTemplate
TargetType
=
"{x:Type DataGridCell}"
><
br
> <
Grid
Background
=
"{TemplateBinding Background}"
><
br
> <
ContentPresenter
VerticalAlignment
=
"Center"
/><
br
> </
Grid
><
br
> </
ControlTemplate
><
br
> </
Setter.Value
><
br
> </
Setter
><
br
> </
Style
><
br
> <
br
> <
MarketWatch:Market
x:Key
=
"Market"
></
MarketWatch:Market
><
br
> </
UserControl.Resources
><
br
> <
Grid
DataContext
=
"{StaticResource Market}"
><
br
> <
Grid.RowDefinitions
><
br
> <
RowDefinition
Height
=
"20"
></
RowDefinition
><
br
> <
RowDefinition
Height
=
"21*"
></
RowDefinition
><
br
> </
Grid.RowDefinitions
><
br
> <
br
> <
Grid
Grid.Row
=
"0"
><
br
> <
Grid.ColumnDefinitions
><
br
> <
ColumnDefinition
Width
=
"5*"
/><
br
> <
ColumnDefinition
Width
=
"6*"
/><
br
> </
Grid.ColumnDefinitions
><
br
> <
Canvas
Grid.Column
=
"1"
><
br
> <
br
> <
telerik:RadComboBox
x:Name
=
"cb_View"
SelectionChanged
=
"cb_View_SelectionChanged"
Width
=
"80"
Canvas.Right
=
"10"
Canvas.Top
=
"5"
Grid.Column
=
"1"
SelectedIndex
=
"0"
><
br
> <
telerik:RadComboBoxItem
Content
=
"Company"
/><
br
> <
telerik:RadComboBoxItem
Content
=
"Sector"
/><
br
> </
telerik:RadComboBox
><
br
> <
telerik:RadComboBox
x:Name
=
"cb_Currency"
Canvas.Right
=
"95"
Canvas.Top
=
"5"
Width
=
"60"
/><
br
> <
TextBox
Name
=
"txt_Rate"
Text
=
"{Binding ChangePrev}"
Canvas.Top
=
"5"
Canvas.Right
=
"160"
Width
=
"60"
TextChanged
=
"txt_Rate_TextChanged"
KeyDown
=
"txt_Rate_KeyDown"
></
TextBox
><
br
> </
Canvas
><
br
> </
Grid
><
br
><
br
> <
telerik:RadGridView
x:Name
=
"grd_Result_Sector"
<br> GroupRenderMode="Flat"<
br
><
span
class
=
"Apple-tab-span"
style
=
"white-space:pre"
> </
span
> AutoGenerateColumns="False"<
br
><
span
class
=
"Apple-tab-span"
style
=
"white-space:pre"
> </
span
> CanUserFreezeColumns="True"<
br
> CanUserDeleteRows="True"<
br
> CanUserInsertRows="False"<
br
> IsReadOnly ="True"<
br
> Grid.Row="1"<
br
><
span
class
=
"Apple-tab-span"
style
=
"white-space:pre"
> </
span
> RowIndicatorVisibility="Collapsed"<
br
> BorderThickness="1"<
br
> BorderBrush="#3090C7"<
br
> Visibility="Hidden"<
br
> ItemsSource="{Binding BindingList_CurrentSectorWatch}"<
br
><
span
class
=
"Apple-tab-span"
style
=
"white-space:pre"
> </
span
> IsFilteringAllowed="True" Margin="1,10,1,1" ShowGroupPanel="False"<
br
> AlternationCount="2"><
br
> <
telerik:RadGridView.Columns
><
br
> <
telerik:GridViewDataColumn
Header
=
"Name"
DataMemberBinding
=
"{Binding CompanyName}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Code"
DataMemberBinding
=
"{Binding SymbolID,StringFormat={}{0:0.####}}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Change"
CellTemplate
=
"{StaticResource StockChangeTemplate}"
DataMemberBinding
=
"{Binding Change}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Change Percentage"
CellTemplate
=
"{StaticResource StockChangePercTemplate}"
DataMemberBinding
=
"{Binding ChangePerc}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
><
br
> <
telerik:GridViewDataColumn
Header
=
"Money Value"
CellTemplate
=
"{StaticResource StockMoneyValueTemplate}"
DataMemberBinding
=
"{Binding MoneyValue}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Trades"
CellTemplate
=
"{StaticResource StockTradesTemplate}"
DataMemberBinding
=
"{Binding _trades}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Transaction Price"
CellTemplate
=
"{StaticResource StockTransactionPriceTemplate}"
DataMemberBinding
=
"{Binding _transactionPrice}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Volume"
CellTemplate
=
"{StaticResource StockVolumeTemplate}"
DataMemberBinding
=
"{Binding Volume}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> </
telerik:RadGridView.Columns
><
br
><
br
> </
telerik:RadGridView
><
br
> <
telerik:RadGridView
x:Name
=
"grd_Result_Company"
GroupRenderMode
=
"Flat"
<br><
span
class
=
"Apple-tab-span"
style
=
"white-space:pre"
> </
span
> AutoGenerateColumns="False"<
br
><
span
class
=
"Apple-tab-span"
style
=
"white-space:pre"
> </
span
> CanUserFreezeColumns="True"<
br
> CanUserDeleteRows="True"<
br
> CanUserInsertRows="False"<
br
> IsReadOnly ="True"<
br
> Grid.Row="1"<
br
><
span
class
=
"Apple-tab-span"
style
=
"white-space:pre"
> </
span
> RowIndicatorVisibility="Collapsed"<
br
> BorderThickness="1"<
br
> BorderBrush="#3090C7"<
br
> Visibility="Visible"<
br
> ItemsSource="{Binding BindingList_CurrentCompanyWatch}"<
br
><
span
class
=
"Apple-tab-span"
style
=
"white-space:pre"
> </
span
> IsFilteringAllowed="True" Margin="1,10,1,1" ShowGroupPanel="False"<
br
> AlternationCount="2"><
br
> <
telerik:RadGridView.Columns
><
br
>
<!--<telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding CompanyName}" Width="*" />-->
<
br
> <
telerik:GridViewDataColumn
Header
=
"Code"
DataMemberBinding
=
"{Binding SymbolID}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Change"
CellTemplate
=
"{StaticResource StockChangeTemplate}"
DataMemberBinding
=
"{Binding Change}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Change Percentage"
CellTemplate
=
"{StaticResource StockChangePercTemplate}"
DataMemberBinding
=
"{Binding ChangePerc}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
><
br
> <
telerik:GridViewDataColumn
Header
=
"Money Value"
CellTemplate
=
"{StaticResource StockMoneyValueTemplate}"
DataMemberBinding
=
"{Binding MoneyValue}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Trades"
CellTemplate
=
"{StaticResource StockTradesTemplate}"
DataMemberBinding
=
"{Binding _trades}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Transaction Price"
CellTemplate
=
"{StaticResource StockTransactionPriceTemplate}"
DataMemberBinding
=
"{Binding _transactionPrice}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> <
telerik:GridViewDataColumn
Header
=
"Volume"
CellTemplate
=
"{StaticResource StockVolumeTemplate}"
DataMemberBinding
=
"{Binding Volume}"
DataFormatString
=
"{}{0:C}"
Width
=
"*"
/><
br
> </
telerik:RadGridView.Columns
><
br
> </
telerik:RadGridView
><
br
><
br
><
br
><
br
> </
Grid
><
br
></
UserControl
><
br
><
div
><
br
></
div
><
br
></
div
>
I Only Declare Market Object :
Market objMarket = new Market();