WPF中的列表框分隔符和最终分隔符的省略

时间:2014-03-28 05:20:48

标签: c# wpf xaml listbox

我有两件事我想要实现:

  1. 在WPF中的列表框项之间添加水平分隔符。
  2. 不要在最终列表框项目的底部显示分隔符。
  3. 我目前的布局如下图所示:

    enter image description here

    这显示了2个列表框项目(虽然我无法知道可能生成多少项目)。我希望它们用水平分隔符分隔,除了最后一个列表框项目,因此窗格底部没有备用分隔符。我怎样才能在XAML中实现这一目标?在这里查看我当前的XAML:

     <TabItem Header="Third Party Updates">
                <Grid>
                    <TextBlock Name="ThirdPartyNoManifestTextBox" Width="Auto" HorizontalAlignment="Left" Margin="267,22,0,0" TextWrapping="Wrap" Text="{Binding Path=WindowsUpdateCompliance, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" FontSize="14" Foreground="DarkSlateBlue"/>
                    <Button Name="CheckforThirdPartyUpdatesButton" Content="Check for Third Party Updates" Margin="10,11,339,304" Click="CheckforThirdPartyUpdatesButton_Click" MaxWidth="200" Grid.Column="1" Grid.Row="1"/>
                    <ListBox Name="ThirdPartyListBox" ItemsSource="{Binding}" Margin="0,70,0,0">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Button Name="ThirdPartyInstallButton" Content="Install" Click="InstallThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"></Button>
                                    <Button Name="ThirdPartyPostoneButton" Content="Postpone" Click ="PostponeThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"></Button>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition />
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition />
                                            <RowDefinition />
                                            <RowDefinition />
                                            <RowDefinition />
                                            <RowDefinition />
                                        </Grid.RowDefinitions>
                                        <Label Content="•" Grid.Row="1" VerticalContentAlignment="Center"/>
                                        <Label Content="•" Grid.Row="2" VerticalContentAlignment="Center"/>
                                        <Label Content="•" Grid.Row="3" VerticalContentAlignment="Center"/>
                                        <Label Content="•" Grid.Row="4" VerticalContentAlignment="Center"/>
                                        <StackPanel Orientation="Horizontal" Grid.Column="1">
                                            <Label Name="MissingRequiredAppGenericTextBlock" VerticalAlignment="Center" Content="Required application update detected:" FontWeight="SemiBold" FontSize="12"/>
                                            <Label Name="RequiredAppNameTextBlock" VerticalAlignment="Center" Content="{Binding Item2.Name}" Foreground="MidnightBlue" FontSize="13"/>
                                            <Label Grid.Column="1" Grid.Row="1" Name="RequiredAppVersionTextBlock" Content="{Binding Item2.RequiredVersion}" VerticalAlignment="Center" Foreground="MidnightBlue" FontSize="13"/>
                                        </StackPanel>
                                        <TextBlock Grid.Column="1" Grid.Row="1" Name="RequiredAppCustomUIMessageTextBlock" Text="{Binding Item2.CustomUIMessage}" TextWrapping="Wrap" VerticalAlignment="Center"/>
                                        <TextBlock Grid.Column="1" Grid.Row="2" VerticalAlignment="Center">
                                        <Hyperlink Name="Link" NavigateUri="{Binding Item2.TT}" RequestNavigate="Hyperlink_RequestNavigate">
                                            <TextBlock Text="{Binding Item2.TT}"/>
                                        </Hyperlink>
                                    </TextBlock>
                                        <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="3">
                                            <TextBlock Text="The following processes will be closed prior to install: " VerticalAlignment="Center" />
                                            <TextBlock Text="{Binding Item2.ListOfProcessesToClose}" FontWeight="SemiBold" Foreground="Red" VerticalAlignment="Center"/>
                                        </StackPanel>
                                        <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="4">
                                            <TextBlock Text="You have used " VerticalAlignment="Center" />
                                            <TextBlock Text="{Binding Item3.UsedDeferrals}" VerticalAlignment="Center"/>
                                            <TextBlock Text=" of " VerticalAlignment="Center"/>
                                            <TextBlock Text="{Binding Item2.MaxDefferals}" VerticalAlignment="Center"/>
                                            <TextBlock Text=" deferrals for this update." VerticalAlignment="Center"/>
                                        </StackPanel>
                                    </Grid>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                        <ListBox.ItemContainerStyle>
                            <Style TargetType="{x:Type ListBoxItem}">
                                <Style.Triggers>
    
                                    <DataTrigger Binding="{Binding PostponeClicked}" Value="1">
                                        <Setter Property="Visibility" Value="Hidden"></Setter>
                                    </DataTrigger>
    
                                    <Trigger Property="Control.IsMouseOver" Value="True">
                                        <Setter Property="Control.BorderBrush" Value="SteelBlue" />
                                        <Setter Property="Control.BorderThickness" Value="1" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </ListBox.ItemContainerStyle>
                    </ListBox>
                </Grid>
            </TabItem>
    

    更新

    添加了建议的分隔符代码。分隔符现在存在但没有填充可用的水平空间:

    enter image description here

1 个答案:

答案 0 :(得分:11)

您可以尝试将Separator放在每个项目的顶部。因此,您在最后一项之后没有不需要的Separator

然后使用带有DataTrigger绑定的{RelativeSource PreviousData}隐藏第一项顶部的分隔符:

<StackPanel>
    <Separator>
        <Separator.Style>
            <Style TargetType="Separator">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Separator.Style>
    </Separator>
    <StackPanel Orientation="Horizontal">
        <Button Name="ThirdPartyInstallButton" Content="Install" Click="InstallThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"></Button>
        <Button Name="ThirdPartyPostoneButton" Content="Postpone" Click ="PostponeThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"></Button>
        <Grid>
            .........
            .........
        </Grid>
    </StackPanel>
</StackPanel>

更新:

我无法确定是什么原因导致分隔符不会在列表框宽度上拉伸。也许尝试将listboxitem的HorizontalContentAlignment设置为Stretch

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    </Style>
</ListBox.ItemContainerStyle>