locked
Hide (but not remove) ListView Item by Binding RRS feed

  • Question

  • Hello,

    I have an ObservableCollection<Book>, and a ListView what is bound to that list. I have to hide some of this ListViewItem, by Book class IsVisible property. There is the Xaml, what can help:

    <ListView ItemsSource="{Binding Book}">
         <ListView.ItemTemplate>
             <DataTemplate>
                  <TextBlock Text="{Binding Name}" Visible="{Binding IsVisible}"/>
             </DataTemplate>
         </ListView.ItemTemplate>
    </ListView>

    It just hide the content of the ListViewItem the TextBlock :(
    If I have 5 element in the list, but 2 have IsVisible="false", then  wanna see just 3 at the GUI, but the list must be untouched.

    Can anybody help? :)
    Best Regards,
    Krekkon

    Thursday, October 3, 2013 5:43 PM

Answers

  • Hi Krekkon,

    You can easily achieve this requirement by editing the control template of ListViewItem. Please check the sample in below link which is created to achieve your requirement,

    Sample: http://sdrv.ms/1bOCon6

    Feel free to ask your questions.

    Regards,

    Riyaj Ahamed I

    • Marked as answer by Anne Jing Thursday, October 10, 2013 9:12 AM
    Monday, October 7, 2013 5:18 AM

All replies

  • What language are you using? Let us know and we can move you to the right forum.
    Thursday, October 3, 2013 6:34 PM
    Moderator
  • O, Sorry :)
    I use C#, and i thank this forum what I need, cause it is more design question than C#. :)
    Friday, October 4, 2013 6:05 PM
  • Hi Krekkon,

    The UI Design for Windows Store apps forum is for language agnostic questions about how the UI should be designed, not how it should be implemented. Appropriate topics will apply equally to Xaml and HTML apps.

    Since your question is about how to implement your design in Xaml and C# it is more appropriate in the language specific forum, and you'll find more people there who can answer it.

    All that said, you'll probably be best off binding to an intermediate CollectionViewSource which contains just the elements you want in your list rather than binding to a list and trying to hide individual elements. This intermediate list can also be used for sorting.

    Other than that you could try using a DataTemplateSelector to switch to an empty template for the items you don't want to use, but I'm not sure offhand if this will work to alter the size of the element. It likely will provide odd data to screen readers if you don't explicitly override the automation properties.

    --Rob

    Saturday, October 5, 2013 12:56 AM
    Moderator
  • Hi Krekkon,
    Maybe this is not what you want but it solves the problem.

    just when  binding your list add a where condition to bind to the visible items only.

     List<Book> bookList = new List<Book>();
                bookList.Add(new Book() { Name = "test", IsVisible = Visibility.Visible });
                bookList.Add(new Book() { Name = "test1", IsVisible = Visibility.Visible });
                bookList.Add(new Book() { Name = "test2", IsVisible = Visibility.Collapsed });
                myListView.ItemsSource = bookList.Where(i => i.IsVisible == Windows.UI.Xaml.Visibility.Visible);

     <ListView Name="myListView" ItemsSource="{Binding}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    Regards,

    Ibraheem Osama Mohamed | My Blog | @IbraheemOM | My Website

    (If my reply answers your question, please propose it as an answer)

    Saturday, October 5, 2013 5:00 AM
  • Hi  Oleg,

    I Try these samples, what you linked. But my problem is that:
    The "hide" is work, but the hided elemnts are still in my ListView, i can click to them. Just i doesnt see them :)
    So it almost work, but just close :(

    Ibrahem,
    You solution is works, but as you thank that, it is not what i want :)
    I try solve it with the best way, what is looks like Oleg's reply. But they are still there, just invisible.
    Later maybe i post an image, if you cant image it :D

    Sunday, October 6, 2013 12:17 PM
  • Hi Krekkon,

    You can easily achieve this requirement by editing the control template of ListViewItem. Please check the sample in below link which is created to achieve your requirement,

    Sample: http://sdrv.ms/1bOCon6

    Feel free to ask your questions.

    Regards,

    Riyaj Ahamed I

    • Marked as answer by Anne Jing Thursday, October 10, 2013 9:12 AM
    Monday, October 7, 2013 5:18 AM