Hello其他程序员!
我试图在ListBox中隐藏第一个ListBoxItem,我做错了什么?我疯狂地搜索,不能让它工作。如果有人可以帮助,将是一种乐趣:)。 ......但没有任何东西被隐藏起来:(。
<ListBox x:Name="DemoItemsListBox" Margin="0 16 0 16" SelectedIndex="0" AlternationCount="2147483647"
ItemsSource="{Binding NavItems}"
PreviewMouseLeftButtonUp="UIElement_OnPreviewMouseLeftButtonUp"
Cursor="Hand">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}" Value="0">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="domain:DemoItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<materialDesign:PackIcon Kind="{Binding PackIconName}" Height="24" Width="24" Grid.Row="0" Grid.Column="0" />
<TextBlock Text="{Binding Name}" Margin="32 0 32 0" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
修改 我得到它部分工作,第一项是不可见的,但我仍然可以点击它并在悬停时看到它。它也消耗空间。
<ListBox x:Name="DemoItemsListBox" Margin="0 16 0 16" SelectedIndex="0" AlternationCount="2147483647"
ItemsSource="{Binding NavItems}"
PreviewMouseLeftButtonUp="UIElement_OnPreviewMouseLeftButtonUp"
Cursor="Hand">
<ListBox.ItemTemplate>
<DataTemplate DataType="domain:DemoItem">
<Grid>
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Style.Triggers>
<DataTrigger Binding="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}" Value="0">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<materialDesign:PackIcon Kind="{Binding PackIconName}" Height="24" Width="24" Grid.Row="0" Grid.Column="0" />
<TextBlock Text="{Binding Name}" Margin="32 0 32 0" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
编辑2 我让它像这样工作。如何使用键来应用Style?我试过像x:Key =&#34; MyCoolStyle&#34;但我找不到放在哪里。
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</Trigger>
</Style.Triggers>
</Style>
答案 0 :(得分:0)
将Visiblity设置为Collaplity未显示且不占用空间。
答案 1 :(得分:0)
您可以像这样使用,
<ListBox x:Name="DemoItemsListBox" Margin="0 16 0 16" SelectedIndex="0" AlternationCount="2147483647"
ItemsSource="12345678"
Cursor="Hand">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="domain:DemoItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding}" Margin="32 0 32 0" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
为方便起见,我更改了ItemsSource。