简体   繁体   中英

collapsed a ListItem from a listView

I have a list view which contain a stack panel. which contain a data grid.

I want that the user will click on a check box. and some of the data will collapsed. (and rest of the data will go up) for any list view Item

will be appericate some help with code exmpale:

attching some of my XAML code:

<Style x:Key="ShowHideStyle" TargetType="StackPanel" >
    <Style.Setters>
        <Setter Property="Visibility" Value="Collapsed" />
    </Style.Setters>
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Value="True">
                    <Condition.Binding>
                        <MultiBinding Converter="{StaticResource V_converter }">
                            <Binding Path="Type"></Binding>
                            <Binding Path="NodeID"></Binding>
                            <Binding Path="TLV"></Binding>
                        </MultiBinding>
                    </Condition.Binding>
                </Condition>
            </MultiDataTrigger.Conditions>

            <MultiDataTrigger.Setters>
                <Setter Property="Visibility" Value="Visible" />
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

<ListView x:Name="LV" ItemsSource= "{Binding  Lggv}" SelectionChanged="dataGridData_SelectionChanged" ItemContainerStyle="{StaticResource ListViewItemStyle}">
    <ListView.ItemTemplate>
        <DataTemplate x:Name="DT" >
            <StackPanel x:Name="LVSP" DataContextChanged="LVSP_DataContextChanged" SourceUpdated="LVSP_SourceUpdated" Style="{StaticResource ShowHideStyle}">
                <Border BorderThickness="1" BorderBrush="Black"  >
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="150"></ColumnDefinition>
                            <ColumnDefinition Width="55"></ColumnDefinition>
                            <ColumnDefinition Width="1*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="tbHeader" Text="{Binding Info }" AllowDrop="True"   FontWeight="Bold" Grid.Column="2" Visibility="{Binding Visibility_Header}" >

                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <!-- this is the default background-->
                                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                <GradientStop Color="#FFCEE6C6" Offset="0.008"/>
                                                <GradientStop Color="#FF9ECF8C" Offset="0.987"/>
                                            </LinearGradientBrush>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </Grid>
                </Border>
                <Grid x:Name="GridData">
                    <Grid.Background>
                        <MultiBinding Converter="{StaticResource converter }">
                            <Binding Path="Type"></Binding>
                            <Binding Path="NodeID"></Binding>
                            <Binding Path="TLV"></Binding>
                        </MultiBinding>
                    </Grid.Background>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150"></ColumnDefinition>
                        <ColumnDefinition Width="55"></ColumnDefinition>
                        <ColumnDefinition Width="1*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0" BorderThickness="1" BorderBrush="Black">
                        <TextBlock Text="{Binding DateTime}"  ></TextBlock>
                    </Border>
                    <Border Grid.Column="1" BorderThickness="1" BorderBrush="Black">
                        <TextBlock Text="{Binding ComPort}"></TextBlock>
                    </Border>
                    <Border Grid.Column="2" BorderThickness="1" BorderBrush="Black">
                        <TextBlock Text="{Binding Data}" ></TextBlock>
                    </Border>
                </Grid>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

You should set the ItemContainerStyle for the targettype ListViewItem instead of targeting your item content ( StackPanel ).

In Resources section, define your style

<Style x:Key="ShowHideStyle" TargetType="ListViewItem">
    ...
</Style>

Then use it

<ListView ItemContainerStyle="{StaticResource ShowHideStyle}" .../>

Ofcourse, I assume that your converter/trigger thing is actually working and you just have the problem of properly hiding the whole list entry.

Put the StackPanel in an Expander and apply the style to the latter.

<ListView x:Name="LV" ItemsSource= "{Binding  Lggv}" SelectionChanged="dataGridData_SelectionChanged" 
                  ItemContainerStyle="{StaticResource ListViewItemStyle}">
    <ListView.ItemTemplate>
        <DataTemplate x:Name="DT" >
            <Expander Header="..." Style="{StaticResource ShowHideStyle}">
                <StackPanel x:Name="LVSP" DataContextChanged="LVSP_DataContextChanged" SourceUpdated="LVSP_SourceUpdated">
                    <Border...></Border...>
                    <Grid...></Grid...>
                </StackPanel>
            </Expander>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Instead of setting the Visbility property to Collapsed/Visible you set the IsExpanded property of the Expander to false/true:

<Style x:Key="ShowHideStyle" TargetType="Expander" >
    <Style.Setters>
        <Setter Property="IsExpanded" Value="False" />
    </Style.Setters>
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Value="True">
                    <Condition.Binding>
                        <MultiBinding Converter="{StaticResource V_converter }">
                            <Binding Path="Type"></Binding>
                            <Binding Path="NodeID"></Binding>
                            <Binding Path="TLV"></Binding>
                        </MultiBinding>
                    </Condition.Binding>
                </Condition>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="IsExpanded" Value="True" />
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM