Hide ListViewItem in WPF ListView

How to hide ListViewItem in linked ListView? Note. I do not want to delete it.

+3
source share
5 answers

Yes, that’s easy.

The first thing you need to do is add the property to the class you are binding to. For example, if you bind to the User class with FirstName and LastName, just add the Boolean IsSupposedToShow property (you can use any property that you like, of course). Like this:

class User: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string FirstName { get; set; }
    public string LastName { get; set; }

    private bool m_IsSupposedToShow;
    public bool IsSupposedToShow
    {
        get { return m_IsSupposedToShow; }
        set
        {
            if (m_IsSupposedToShow == value)
                return;
            m_IsSupposedToShow = value;
            if (PropertyChanged != null)
                PropertyChanged(this, 
                    new PropertyChangedEventArgs("IsSupposedToShow"));
        }
    }
}

, - , - , , ! . , , , (, ) - . XAML .

:

<DataTemplate DataType="{x:Type YourType}">
    <DataTemplate.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSupposedToShow}" Value="False">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataTemplate.Resources>
    <!-- your UI here -->
    <TextBlock> 
        <TextBlock.Text> 
        <MultiBinding StringFormat="{}{0}, {1}"> 
            <Binding Path="LastName" /> 
            <Binding Path="FirstName" /> 
        </MultiBinding> 
        </TextBlock.Text>
    </TextBlock>
</DataTemplate>

IsSupposedToShow false, XAML , DataTemplate. WPF , , !

!

+9

, .

+3
<ItemsControl>
 <ItemTemplate>
  <DataTemplate>
   <Image Visibility='{Binding Converter=my:MaybeHideThisElementConverter}' />
   </Image>
  </DataTemplate>
 </ItemTemplate>
</ItemsControl>

, MaybeHideThisElementConverter. Collapsed, User NULL, Count , . , Visibility.Collapsed, Visibility.Visible .

+1

, , :

  • ListView.ItemContainerStyle DataTrigger Visibility .
  • ItemTemplate DataTemplate , .
  • ItemsSource ListView CollectionView CollectionView Filter . . MSDN .
  • ObservableCollection ItemsSource ListView / .

ValueConverter, .

, CollectionView, , , , .

+1

This page gave me the answer I need: http://www.abhisheksur.com/2010/08/woring-with-icollectionviewsource-in.html (see the "Filtering" section.)

Wow, a lot easier than XAML.

Example:

bool myFilter(object obj)
{
    // Param 'obj' comes from your ObservableCollection<T>.
    MyClass c = obj as MyClass;
    return c.MyFilterTest();
}

// apply it
myListView.Items.Filter = myFilter;
// clear it
myListView.Items.Filter = null;
+1
source

Source: https://habr.com/ru/post/1772201/


All Articles