locked
Using CollectionViewSource sets the SelectedItem on populate of ItemsSource RRS feed

  • Question

  • I have made a small sample to show my problem which is that when I use a CollectionViewSource to allow grouping when I set the ItemsSource property in VM it sets the SelectedItem for that collection.  Since when the SelectedItem is set there is a whole bunch of processing it should not happen until the user actually selects an Item.  This happens for both ListView and DataGrid (tested).

    The following is the XAML for the sample:

    <Window x:Class="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:TestSelectedItem"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800" Initialized="Window_Initialized">
    	<Window.Resources>
    		<CollectionViewSource x:Key="ListBoxItems" Source="{Binding Path=Stuff}">
    			<CollectionViewSource.GroupDescriptions>
    				<PropertyGroupDescription PropertyName="Name" />
    			</CollectionViewSource.GroupDescriptions>
    		</CollectionViewSource>
    
    	</Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
    			<RowDefinition></RowDefinition>
    		</Grid.RowDefinitions>
    
    		<DataGrid x:Name="dg" ItemsSource="{Binding Source={StaticResource ListBoxItems}}" CanUserAddRows="False"  SelectedItem="{Binding aTestClass}" SelectionMode="Single" ></DataGrid>
    
    		<!--<DataGrid x:Name="dg" ItemsSource="{Binding Stuff}" CanUserAddRows="False"  SelectedItem="{Binding aTestClass}" SelectionMode="Single" IsSynchronizedWithCurrentItem="True" ></DataGrid>-->
    
    		<TextBlock Grid.Row="1" Text="{Binding aTestClass}"></TextBlock>
    
    		<Button Grid.Row="2" Click="Button_Click">Test it</Button>
        </Grid>
    </Window>

    When I use the above the SelectedItem is set when the button is clicked and the Stuff collection is populated.  If I comment out that line and uncomment the line which directly uses the Stuff property the SelectedItem is not set.

    The actual data in the collection is not relevant.

    Any ideas??


    Lloyd Sheen


    • Edited by sqlguy Tuesday, December 10, 2019 10:39 PM
    Tuesday, December 10, 2019 10:39 PM

All replies

  • Hi,

    I try and fix your code,but without the problem you said.

      public partial class MainWindow : Window
        {
            ViewMode vm = new ViewMode();
    
            public MainWindow()
            {
                InitializeComponent();
    
                this.DataContext = vm;
            }
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                vm.Stuff.Add(new Student()
                {
                    Id = 1,
                    Name = "Name1",
                    Uid = 1
                });
    
                vm.Stuff.Add(new Student()
                {
                    Id = 3,
                    Name = "Name3",
                    Uid = 2
                });
    
                vm.Stuff.Add(new Student()
                {
                    Id = 2,
                    Name = "Name2",
                    Uid = 1
                });
                dg.Items.Refresh();
            }
        }
    
        public class ViewMode : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private Student _aTestClass;
            public Student aTestClass
            {
                get { return _aTestClass; }
                set
                {
    
                    _aTestClass = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("aTestClass"));
                }
    
            }
            public List<Student> _Stuff = new List<Student>();
            public List<Student> Stuff 
            {
                get { return _Stuff; }
                set { _Stuff = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Stuff")); }
            }
        }
        public class Student
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Uid { get; set; }
        }
    <Window.Resources>
            <CollectionViewSource x:Key="ListBoxItems" Source="{Binding Path=Stuff}">
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="Name" />
                </CollectionViewSource.GroupDescriptions>
            </CollectionViewSource>
    
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
    
            <!--<DataGrid x:Name="dg" ItemsSource="{Binding Source={StaticResource ListBoxItems}}" CanUserAddRows="False"  SelectedItem="{Binding aTestClass}" SelectionMode="Single" ></DataGrid>-->
    
            <DataGrid x:Name="dg" ItemsSource="{Binding Stuff}" CanUserAddRows="False"  SelectedItem="{Binding aTestClass}" SelectionMode="Single" IsSynchronizedWithCurrentItem="True" ></DataGrid>
    
            <TextBox Grid.Row="1" Text="{Binding aTestClass.Name}"></TextBox>
    
            <Button Grid.Row="2" Click="Button_Click">Test it</Button>
        </Grid>


    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 11, 2019 8:43 AM
  • You are not getting a problem because your sample is not using the CollectionViewSource.  Comment out the second DataGrid and uncomment the first and you will see the problem.

    Lloyd Sheen

    Wednesday, December 11, 2019 1:11 PM