Click here to Skip to main content
14,265,784 members

Silverlight / WPF

 
AnswerRe: ListBox of Hyperlinks - Selected Item Pin
Mycroft Holmes7-Jul-18 13:38
memberMycroft Holmes7-Jul-18 13:38 
AnswerRe: ListBox of Hyperlinks - Selected Item Pin
Richard Deeming9-Jul-18 8:32
mveRichard Deeming9-Jul-18 8:32 
QuestionWhat is Silverlight / WPF??? Pin
Arnav12130-Jun-18 20:03
memberArnav12130-Jun-18 20:03 
AnswerRe: What is Silverlight / WPF??? Pin
Richard MacCutchan30-Jun-18 20:50
protectorRichard MacCutchan30-Jun-18 20:50 
QuestionNested Borders Mouse Over Pin
Kevin Marois14-Jun-18 7:44
professionalKevin Marois14-Jun-18 7:44 
AnswerRe: Nested Borders Mouse Over Pin
Richard Deeming14-Jun-18 9:35
mveRichard Deeming14-Jun-18 9:35 
GeneralRe: Nested Borders Mouse Over Pin
Kevin Marois14-Jun-18 10:51
professionalKevin Marois14-Jun-18 10:51 
QuestionWPF DataGrid uses a lot of memory, or is slow to scroll Pin
Leif Simon Goodwin14-Jun-18 5:16
memberLeif Simon Goodwin14-Jun-18 5:16 
I have a problem with a DataGrid consuming a huge amount of memory when we have ~15,000 cells, each cell holding an edit control ie CheckBox, TextBox or ComboBox. The view takes 40 seconds to appear, and then consumes 1GB RAM. I can turn on virtualisation of rows and columns, which cures the slow start, and reduces memory by a factor of 3. However, that makes scrolling unacceptably slow ie many seconds.

I have a test app which allows me to test methods to improve performance. It displays values in a 2D matric, which is defined as a list of Row instance. Each Row object contains an array of node instances which hold the values.


The DataGrid is simple enough:

<DataGrid Grid.Row="2" Name="_dataGrid2" 
                  ItemsSource="{Binding Matrix}" 
                  AlternationCount="2" 
                  AutoGenerateColumns="False" 
                  Style="{StaticResource styleDataGridNoSelection}" 
                  EnableRowVirtualization="True" 
                  EnableColumnVirtualization="True"
                  VirtualizingStackPanel.IsVirtualizing="False"
                  VirtualizingStackPanel.VirtualizationMode="Recycling"
                  ScrollViewer.CanContentScroll="True"
                  HeadersVisibility="None"/>


The view constructor creates the column data templates:

public MainWindow()
        {
            InitializeComponent();

            MainViewModel viewModel = new MainViewModel();
            DataContext = viewModel;

            //_gridDataTemplateSelector = new TouchHub2.View.DataGridTemplateSelector();
            _gridDataTemplateSelector = new GridDataTemplateSelector();
            _gridDataTemplateSelector.LabelDataTemplate = Resources["LabelDataTemplate"] as DataTemplate;
            _gridDataTemplateSelector.TextBoxDataTemplate = Resources["TextBoxDataTemplate"] as DataTemplate;
            _gridDataTemplateSelector.CheckBoxDataTemplate = Resources["CheckBoxDataTemplate"] as DataTemplate;
            _gridDataTemplateSelector.ComboBoxDataTemplate = Resources["ComboBoxDataTemplate"] as DataTemplate;

            for (int i = 0; i < Row.constColumnCount; i++)
            {
                DataGridTemplateColumn col = new DataGridTemplateColumn();

                FrameworkElementFactory fef = new FrameworkElementFactory(typeof(ContentPresenter));
                Binding binding = new Binding();
                fef.SetBinding(ContentPresenter.ContentProperty, binding);
                fef.SetValue(ContentPresenter.ContentTemplateSelectorProperty, _gridDataTemplateSelector);

                binding.Path = new PropertyPath("Values[" + i + "]");
                DataTemplate dataTemplate = new DataTemplate();
                dataTemplate.VisualTree = fef;
                col.CellTemplate = dataTemplate;

                _dataGrid2.Columns.Add(col);
        }


The view model is also simple:

public class MainViewModel : System.ComponentModel.INotifyPropertyChanged
    {
        public MainViewModel()
        {
            const int RowCount = 150;

            _matrix = new Row[RowCount];
            for (int i = 0; i < RowCount; i++)
            {
                _matrix[i] = new Row("Row" + i.ToString(), i);
            }
        }

        private Row[] _matrix;
        public Row[] Matrix
        {
            get
            {
                return _matrix;
            }
        }

        #region INotifyPropertyChanged

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        public virtual void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }


And the row is just a simple array of nodes:

public class Row
    {
        private string name;
        private Model.INode[] _values;

        public string Name { get { return name; } }
        public Model.INode[] Values { get { return _values; } }
        public int Index { get; set; }

        public const int constColumnCount = 90;
        public Row(string name, int index)
        {
            Index = index;
            this.name = name;
            _values = new Model.INode[constColumnCount];
            for (int i = 0; i < constColumnCount; i++)
            {
                if (index == 0)
                {
                    _values[i] = new Model.LabelNode();
                    (_values[i] as Model.LabelNode).Value = i.ToString();
                }
                else
                {
                    if (i == 0)
                    {
                        _values[i] = new Model.LabelNode();
                        (_values[i] as Model.LabelNode).Value = index.ToString();
                    }
                    else
                    {
                        switch ((i + index) % 3)
                        {
                            case 0:
                                _values[i] = new Model.TextNode();
                                (_values[i] as Model.TextNode).Value = i.ToString();
                                break;
                            case 1:
                                _values[i] = new Model.CheckBoxNode();
                                (_values[i] as Model.CheckBoxNode).Value = true;
                                break;
                            case 2:
                                _values[i] = new Model.ComboBoxNode();
                                (_values[i] as Model.ComboBoxNode).Values = new List<string>(2) { (i + index).ToString(), (i + index + 1).ToString() };
                                (_values[i] as Model.ComboBoxNode).Value = (_values[i] as Model.ComboBoxNode).Values[0];
                                break;
                        }
                    }
                }
            }
        }
}

The node classes are basic shells eg:

class TextNode : INode
    {
        public string Value { get; set; }
    }

AnswerRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Pete O'Hanlon14-Jun-18 5:21
protectorPete O'Hanlon14-Jun-18 5:21 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Leif Simon Goodwin14-Jun-18 21:18
memberLeif Simon Goodwin14-Jun-18 21:18 
AnswerRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Gerry Schmitz14-Jun-18 8:01
mveGerry Schmitz14-Jun-18 8:01 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Leif Simon Goodwin14-Jun-18 21:59
memberLeif Simon Goodwin14-Jun-18 21:59 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Gerry Schmitz15-Jun-18 8:16
mveGerry Schmitz15-Jun-18 8:16 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Leif Simon Goodwin18-Jun-18 5:40
memberLeif Simon Goodwin18-Jun-18 5:40 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Gerry Schmitz18-Jun-18 7:04
mveGerry Schmitz18-Jun-18 7:04 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Mycroft Holmes19-Jun-18 13:10
memberMycroft Holmes19-Jun-18 13:10 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Leif Simon Goodwin19-Jun-18 22:43
memberLeif Simon Goodwin19-Jun-18 22:43 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Pete O'Hanlon19-Jun-18 22:56
protectorPete O'Hanlon19-Jun-18 22:56 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Leif Simon Goodwin19-Jun-18 23:27
memberLeif Simon Goodwin19-Jun-18 23:27 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Pete O'Hanlon19-Jun-18 23:59
protectorPete O'Hanlon19-Jun-18 23:59 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Mycroft Holmes20-Jun-18 12:04
memberMycroft Holmes20-Jun-18 12:04 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Leif Simon Goodwin21-Jun-18 21:27
memberLeif Simon Goodwin21-Jun-18 21:27 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Mycroft Holmes22-Jun-18 11:55
memberMycroft Holmes22-Jun-18 11:55 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Leif Simon Goodwin25-Jun-18 3:02
memberLeif Simon Goodwin25-Jun-18 3:02 
GeneralRe: WPF DataGrid uses a lot of memory, or is slow to scroll Pin
Pete O'Hanlon25-Jun-18 3:08
protectorPete O'Hanlon25-Jun-18 3:08 

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.