Warm tip: This article is reproduced from serverfault.com, please click

c#-Textblock使用空间折叠

(c# - Textblock collapsed using space)

发布于 2020-11-28 13:55:42

我有一个C#WPF项目。ObservableCollection<T>viewmodel课堂上使用。

我有一个ListViewxaml文件中。还有TextBlock里面的ListView结合ObservableCollection<T>

我想把它们折叠起来DataTrigger结果是那些TextBlock折叠了但仍在使用空间。我想问一下如何解决?

这是ListView我正在使用的:

<ListView Grid.Row="1"  ItemsSource="{Binding ServiceObjects}">
    <ListView.ItemTemplate>
        <DataTemplate>
                <TextBlock Text="{Binding ServiceName}">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Setter Property="Foreground" Value="Black" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsCollapsed}" Value="true">
                                    <Setter Property="Foreground" Value="#FFBF00" />
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

在此处输入图片说明

Questioner
sflee
Viewed
0
Adam Stawarek 2020-11-28 23:59:17

试试这个:

<Style TargetType="ListBoxItem" x:Key="LstBoxItemStyle">
   <Setter Property="MinHeight"
           Value="0" />
   <Setter Property="IsEnabled"
           Value="False" />
   <Style.Triggers>
          <DataTrigger Binding="{Binding IsCollapsed}" Value="True">
               <Setter Property="IsEnabled"
                       Value="True" />
          </DataTrigger>
   </Style.Triggers>
</Style>

  <ListView Grid.Row="1"
            ItemsSource="{Binding ServiceObjects}"
            ItemContainerStyle="{StaticResource LstBoxItemStyle}">

或使用过滤器https://docs.microsoft.com/pl-pl/dotnet/desktop/wpf/data/how-to-filter-data-in-a-view?view=netframeworkdesktop-4.8