Question

I have a listbox and all I want to do is collapse the listboxitem based on a boolean property of my SelectedItem. The IsVisible property on my client Model implements the NotifyPropertyChanged event.

Overview - I have a list of clients which users can do CRUDs on. When they delete, I set a boolean property on the Model which my VM exposes to the View. This should then only hide the 'deleted' row from the list. During a flush to db I CRUD based on the mode of the model.

<ListBox Name="listClients"
                 Grid.Column="1" Grid.Row="1" 
                 Margin="0" BorderThickness="0"                     
                 Height="auto"
                 Style="{StaticResource BumListBox}"
                 SelectionMode="Extended"
                 ItemsSource="{Binding Path=ClientList}"
                 SelectedItem="{Binding SelectedClient, Mode=TwoWay}"
                 Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" >

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsVisible}" Value="False">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="50"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="{Binding ClientNo}" Foreground="White" FontSize="{StaticResource HeaderFontSize}" VerticalAlignment="Center" />
                        <TextBlock Grid.Column="1" Text="{Binding ClientDesc}" Foreground="White" FontSize="{StaticResource SubHeaderFontSize}" FontWeight="Light" VerticalAlignment="Center" />                           
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Code behind to jippo MVVM process:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    if (_cvm.SelectedClient != null)
    {
        _cvm.SelectedClient.IsVisible = !_cvm.SelectedClient.IsVisible;
        _cvm.CurrentSelectedIsVisible = _cvm.SelectedClient.IsVisible;  //<- another       option to bind to
    }
}

I've tried the these suggestions here and here or something similar but I just can't get to hide the items.

Any help in the right direction would be great, cheers.

Edit

I've tried Blam's suggestion below like this but still unable to hide the items:

<ListBox.Resources>
<Style TargetType="ListBoxItem">
    <Setter Property="Visibility" Value="{Binding Path=CurrentIsVisible, Converter={StaticResource b2v}}" />
</Style>

Was it helpful?

Solution 2

This was rather frustrating and the solution so simple. My client model with IsVisible is in a dll and the NotifyPropertyChanged() changes were never built to update the reference in my project..so the binding never happened. These late nights are taking their toll.

OTHER TIPS

You will need to set up a converter if you are returning true/false but there is a system converter for that

Move it up to Resources
I have know I have used it this way

<ListBox x:Name="lb" ItemsSource="{Binding}" DisplayMemberPath="Text">
    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="Visibility" Value="{Binding Path=Vis}" />
        </Style>
    </ListBox.Resources>
</ListBox>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top