locked
Unable to add/remove/clear GridViewItem from GridView programatically when data-binding is used RRS feed

  • Question

  • I have a GridView xaml like this:

     <GridView x:Name="gv" ItemsSource="{Binding}" SelectionChanged="gv_SelectionChanged_1"/>

    I tried removing/adding item after SelectionChanged is called

            private void gv_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
            {
                gv.Items.RemoveAt(0);
            }

    But that throws an exception: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

    RemoveAt, Clear, Remove, Add function for ItemCollection work fine when GridViewItems are specified in the XAML without databinding eg:

     <GridView x:Name="gv" SelectionChanged="gv_SelectionChanged_1">
    <GridViewItem>1</GridViewItem>
    <GridViewItem>2</GridViewItem>
    <GridViewItem>3</GridViewItem>
    <GridViewItem>4</GridViewItem>
    <GridViewItem>5</GridViewItem>
    </GridView>

    Is this a bug or am I doing it wrong? I want to remove the GridViewItem without my data-source being changed. Any help would be greatly appreciated! Thanks!

    Friday, June 1, 2012 7:06 AM

Answers

  • When you are databound via ItemsSource you cannot make collection changes via the GridView.Items (or any ItemsControl.Items) property. You must go through your items source/data source.

    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

    Friday, June 1, 2012 10:42 PM

All replies

  • I'll research this - it seems weird.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    Friday, June 1, 2012 2:16 PM
    Moderator
  • When you are databound via ItemsSource you cannot make collection changes via the GridView.Items (or any ItemsControl.Items) property. You must go through your items source/data source.

    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

    Friday, June 1, 2012 10:42 PM
  • Hi Mark,

    I do agree with you when it comes to the ItemsSource, but how should I handle the SelectedItems collection. The SelectedItems property has only a getter and I don't know how I can bind it to any of my Collections. Do I need to implement something like a IsSelected property in my bounded objects?

    Yours sincerely,

    Darius

    Thursday, January 3, 2013 12:05 PM
  • You can add to the SelectedItems list via the Add like itemGridView.SelectedItems.Add(item); but probably not when databound. I'd add an IsSelected property to your object, the object that implements or inherits of BindableBase etc.  However,  heads-up I think bindings on setters don't work with WinRT GridView's so not sure if you could do something like 

    <GridView.ItemContainerStyle> 
    <Style TargetType="GridViewItem">
    <Setter Property="IsSelected" Value="True"/>
    </Style>
    </GridView.ItemContainerStyle>

    Friday, January 4, 2013 12:45 AM