2

If one were to compile and run the following code, one would find that selecting and/or deselecting a row causes a line to be written to the Output window (as closer inspection of said code would lead one to believe).

After a short time of changing the selected row of the grid using the arrow keys (holding the Up and Down arrows respectively to traverse the entire data set a few times), one would be shocked (as I was) to notice that Output messages cease, even while continuing to cycle through the grid's rows.

I am attempting to achieve something similar to what was given in this answer.

I am absolutely baffled. What would cause Bindings on my grid to spontaneously fail? Any and all help here would be MUCH appreciated!! Also, should anyone have the time to reproduce this, please comment with your findings.

XAML:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <DataGrid Name="TheGrid">
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="IsSelected" 
                            Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn IsReadOnly="True" 
                                    Binding="{Binding Name}" Header="Name"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Code-behind:

using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;

namespace WpfApplication1 {
    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();
            TheGrid.ItemsSource = Enumerable.Range(1, 100)
                                 .Select(i => new MyClass("Item " + i));
        }
    }

    public class MyClass : INotifyPropertyChanged {
        public string Name { get; private set; }

        private bool m_IsSelected;
        public bool IsSelected {
            get {
                return m_IsSelected;
            }
            set {
                if (m_IsSelected != value) {
                    m_IsSelected = value;
                    Console.WriteLine(Name + ": " + m_IsSelected);
                    PropertyChanged(this, 
                        new PropertyChangedEventArgs("IsSelected"));
                }
            }
        }

        public MyClass(string name) {
            Name = name;
        }

        public event PropertyChangedEventHandler PropertyChanged = 
                                                            delegate { };
    }
}

Thanks in advance!

EDIT:

  • Tried applying the DataGridRow Style using the RowStyleSelector property - fail.

  • Tried applying the DataGridRow Style using the Row_Loading and Row_Unloading events - fail.

  • Tried using a custom MultiSelectCollectionView - fail (didn't work with DataGrid control)

  • Tried setting VirtualizingStackPanel.IsVirtualizing="False"- fail (unusably slow with hundreds of rows)

  • Tried messing with VirtualizingStackPanel.VirtualizationMode (Standard or Recycled) - fail.

As stated in one of my comments below, the overarching problem is that I need to bind the SelectedItems property of the DataGrid to my ViewModel, but can't, since SelectedItems is read-only.

There HAS to be some kind of pure-MVVM, out-of-the-box solution for this, but so far, it eludes me!

Community
  • 1
  • 1
Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149
  • I was able to crash VS. Double clicking sometimes brings it back, apparently, as does sorting the row. Probably related to the virtualization thing. – xdumaine Nov 24 '10 at 14:35
  • The over-arching problem is that I can't do multiple selections in a MVVM-centric way using the DataGrid control (SelectedItems is read-only, and therefore is not bindable). This guys's solution seems to be what I need: http://grokys.blogspot.com/2010/07/mvvm-and-multiple-selection-part-iii.html though I haven't implemented it yet. I'll comment back once I have that working (or not working...) – Mark Carpenter Nov 24 '10 at 14:51
  • The solution I linked in my previous comment did not work at all with the WPF DataGrid control, so... no dice. – Mark Carpenter Nov 24 '10 at 16:26
  • Was there a solution to this problem, the accepted answer doesnt work for me, and if I clone the code in a new system it works for some tries – anand_v.singh Mar 09 '21 at 05:40

1 Answers1

1

I just tried this and had the same behavior. I was able to fix the problem by changing the DataGrid to prevent it from virtualizing as follows: <DataGrid Name="TheGrid" AutoGenerateColumns="False" VirtualizingStackPanel.IsVirtualizing="False">.

For more information see this MSDN forum post.

Colin Thomsen
  • 1,806
  • 15
  • 11
  • Awesome! I wonder how my grid will be with large data sets though with virtualization turned off... In any event, that worked! Thanks! – Mark Carpenter Nov 24 '10 at 06:04
  • Just tried it with a large data set, and filtering takes FOREVER! Any idea how I can get around this behavior with virtualizing turned on? – Mark Carpenter Nov 24 '10 at 06:11
  • You could try turning virtualization back on - there are two modes so the other might work: VirtualizingStackPanel.VirtualizationMode=Standard and Recycling – Rune Andersen Nov 24 '10 at 09:05
  • Virtualization behaviour can be really odd sometimes - be sure that your DataGrid is not in a scrollviewer. – Rune Andersen Nov 24 '10 at 09:08
  • I don't have any explicit scrollviewers on the page. (Obviously the contents of the grid will scroll with many items automatically). – Mark Carpenter Nov 24 '10 at 14:37
  • Also, I did try the different VirtualizationModes, but to no avail... Thanks for the suggestions. – Mark Carpenter Nov 24 '10 at 14:37
  • Interestingly, this problem doesn't occur if you use version 3.5 of the framework and the DataGrid in the WPF toolkit. – Robert Rossney Nov 24 '10 at 18:40
  • @Robert - Hmm, that is interesting... unfortunately, I'd like to stay in .NET 4 to take advantage of some of the other built-in controls. Still, strange that that it would work in the Toolkit. Thanks for the info! – Mark Carpenter Nov 25 '10 at 05:59
  • @Colin - Thanks for diagnosing the actual problem. Since that was my original question (why this was happening), I accept your answer! Now to find a workaround without having to resort to events in my code-behind (which I ended up using)... – Mark Carpenter Nov 25 '10 at 06:00