Listbox items do not disappear

I'm trying to hide my listbox items when an event occurs, but using a converter to set the visibility I cannot completely hide them. Instead of disappearing the turn into a thin empty box still visible and selectable on the listbox.

Before hiding: http://i66.tinypic.com/34t96qc.jpg

Alter hiding: http://i66.tinypic.com/rhu23q.png (the box is still visible and clickable)

<ListBox ItemsSource="{Binding MyList}" Name="SourceListBox" SelectionMode="Multiple">
           <ListBox.ItemTemplate>
               <DataTemplate>
                   <ListBoxItem x:Name="lbxItem">   
                    <ListBoxItem.Visibility>
                        <MultiBinding Converter="{StaticResource AvailableItemsConverter}">
                            <Binding Path="Name" />
                            <Binding ElementName="trwEntities" Path="SelectedItem"/>
                        </MultiBinding>
                    </ListBoxItem.Visibility>
                   <TextBlock Text="{Binding Path=Name}" Visibility="{Binding ElementName=lbxItem, Path=Visibility}"/>
               </ListBoxItem>
               </DataTemplate>
           </ListBox.ItemTemplate>
        </ListBox>

I also tried to bind the textblock visibility to the ListBoxItem visibility but nothing changes.

   public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return (...) ? Visibility.Collapsed : Visibility.Visible;
    }

2 answers

  • answered 2018-04-14 14:18 Andy

    You're just making a textblock in the listboxitem collapse.
    You need to make the whole listboxitem collapse OR filter the item out the view.
    Try collapsing the listboxitem first.
    Add a Visibility property to whatever class presents your data for each row. Call that a RowVM. Make sure it's a propfull and raises property changed when set. Then implement whatever your logic is to set it. Your RowVM needs to implement inotifypropertychanged.
    In your listbox, then bind to that.
    Something like:

    <ListBox.ItemContainerStyle>
             <Style TargetType="{x:Type ListBoxItem}">
                   <Setter Property="Visbility" Value="{Binding Visibility}"/>
             </Style>
    </ListBox.ItemContainerStyle>
    

    You put that inside your listbox, insert it just under the opening tag of your listbox.

  • answered 2018-04-14 14:18 Fab

    Add an ItemContainerStyle to set the property (Visibility) using the converter on the style instead.

    <ListBox.ItemContainerStyle>
       <Style TargetType="{x:Type ListBoxItem}">
              <Setter Property="Visibility" >
                     <Setter.Value>
                      <MultiBinding Converter="{StaticResource AvailableItemsConverter}">
                         <Binding Path="Name" />
                         <Binding ElementName="trwEntities" Path="SelectedItem"/>
                      </MultiBinding>
                     </Setter.Value>
             </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
     <ListBox.ItemTemplate>
    

    Get rid of the <ListBoxItem.Visibility> item, you won't need it anymore.