How can I filter this listview and keep my selections?

slim2169 1 Reputation point
2022-11-25T16:23:47.113+00:00

So I've got the following code setup to present a list of items that are imported from an XML file. The user makes selections and then exports their choices to XML. Selecting multiple lines works fine, but if the filter at the top is used, it will uncheck all the existing selections. Not sure if I need to filter it differently or if there's a property that will hold those values. The script is PowerShell and has a XAML file as well.

Powershell search function (filter):
$syncHash.WPFtb_Search.Add_TextChanged( { $syncHash.WPFlv_xml.ItemsSource = $arr.Where( { $_.Acc_Unit -like "*$($syncHash.WPFtb_search.Text)*" } ) } )

XAML:
`<Window x:Class="O365DGMembers.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:O365DGMembers"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
mc:Ignorable="d"
Title="Company Selector" Height="600" Width="710" ResizeMode="CanMinimize" SizeToContent="Width" WindowStartupLocation="CenterScreen">

<Grid Name="maingrid">  
    <Grid.RowDefinitions>  
        <RowDefinition Height="1.5*"></RowDefinition>  
        <RowDefinition Height="10*"></RowDefinition>  
    </Grid.RowDefinitions>  
    <Grid.ColumnDefinitions>  
        <ColumnDefinition Width="*"></ColumnDefinition>  
        <ColumnDefinition Width="*"></ColumnDefinition>  
        <ColumnDefinition Width="*"></ColumnDefinition>  
    </Grid.ColumnDefinitions>  


    <StackPanel Grid.Column="0" FlowDirection="LeftToRight" Grid.Row="0" Height="30" Orientation="Horizontal">  
        <Label Content="Filter :" HorizontalAlignment="Center" Margin="2,2,2,2" Width="60" Height="25" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>  
        <TextBox Name ="tb_search" HorizontalAlignment="Center" Height="25" Width="150" Margin="2,2,2,2" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"></TextBox>  
    </StackPanel>  
    <Button Name="btn_export" Content="Export Selection" Grid.Column="2" HorizontalAlignment="Stretch" Margin="20,2,2,2" FontSize="11" Height="25" Width="100" VerticalContentAlignment="Center"></Button>  
    <ListView Name="lv_xml" Grid.Row="1" Grid.ColumnSpan="3" SelectedValue="{Binding ElementName=cbox_resp, Path=SelectedValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="False" SelectionMode="Multiple">  
        <ListView.View>  
            <GridView>  
                <GridViewColumn Header="Select">  
                    <GridViewColumn.CellTemplate>  
                        <DataTemplate>  
                            <StackPanel Orientation="Horizontal">  
                                <CheckBox Tag="{Binding IsChecked}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />  
                            </StackPanel>  
                        </DataTemplate>  
                    </GridViewColumn.CellTemplate>  
                </GridViewColumn>  
                <GridViewColumn Header="Company" Width="60" DisplayMemberBinding="{Binding Company}"/>  
                <GridViewColumn Header="Company Description" Width="150" DisplayMemberBinding="{Binding CompanyName}"/>  
                <GridViewColumn Header="Acc__Unit" Width="65" DisplayMemberBinding="{Binding 'Acc_Unit'}"></GridViewColumn>  
                <GridViewColumn Header="Description" Width="250" DisplayMemberBinding="{Binding 'Acc_UnitDescription'}"></GridViewColumn>  
                <GridViewColumn Header="Primary Reporting" Width="110">  
                    <GridViewColumn.CellTemplate>  
                        <DataTemplate>  
                            <ComboBox Name="cb_resp" Width="60" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" SelectedItem="{Binding Path=Preporting, Mode=TwoWay}">  
                                <ComboBoxItem IsSelected="True">TRUE</ComboBoxItem>  
                                <ComboBoxItem>FALSE</ComboBoxItem>  
                            </ComboBox>  
                        </DataTemplate>  
                    </GridViewColumn.CellTemplate>  
                </GridViewColumn>  
                <GridViewColumn Header="Responsibility" Width="150">  
                    <GridViewColumn.CellTemplate>  
                        <DataTemplate>  
                            <ComboBox Name="cbox_resp" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" SelectedItem="{Binding Path=Resp, Mode=TwoWay}">  
                                <ComboBoxItem>Executive Director</ComboBoxItem>  
                                <ComboBoxItem>Vice President</ComboBoxItem>  
                                <ComboBoxItem IsSelected="True">Productivity</ComboBoxItem>  
                                <ComboBoxItem>Director</ComboBoxItem>  
                                <ComboBoxItem>Manager</ComboBoxItem>  
                                <ComboBoxItem>Budget</ComboBoxItem>  
                            </ComboBox>  
                        </DataTemplate>  
                    </GridViewColumn.CellTemplate>  
                </GridViewColumn>  
              
            </GridView>  
              
        </ListView.View>  
        <ListView.Resources>  
            <Style TargetType="ListViewItem">  
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />  
            </Style>  
        </ListView.Resources>  
    </ListView>  
</Grid>  

</Window>`

Any ideas?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,823 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
830 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,621 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.