WPF中的列表框分隔符和省略最终分隔符 [英] Listbox Separator in WPF and Omission of Final Separator

查看:86
本文介绍了WPF中的列表框分隔符和省略最终分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现两件事:

  1. 在WPF中的列表框项目之间添加水平分隔符.
  2. 不要在最终列表框项目的底部显示分隔符.

我当前的布局如图所示:

My current layout is pictured here:

这显示了2个列表框项目(尽管我无法知道可能会生成多少个项目).我希望它们之间用水平分隔符分隔,但最后一个列表框项目除外,因此窗格底部没有备用分隔符.如何在XAML中实现这一目标?在此处查看我当前的XAML:

This shows 2 listbox items (though I have no way of knowing how many items could potentially be generated). I would like them separated by a horizontal separator, except on the last listbox item so there isn't a spare separator at the bottom of the pane. How can I achieve this in XAML? See my current XAML here:

 <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>

更新

Update

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

Added suggested separator code. Separator is now present but does not fill the available horizontal space:

推荐答案

您可以尝试将Separator放在每个项目的顶部.这样一来,您在最后一项之后就不会出现不必要的Separator了.

You can try to put Separator at the top of each item. With that you don't have unwanted Separator after the last item.

然后将DataTrigger{RelativeSource PreviousData}绑定一起使用,以将分隔符隐藏在第一项的顶部:

Then use DataTrigger with {RelativeSource PreviousData} binding to hide separator at the top of the first item :

<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:

I can't tell for sure what causes the separator not stretching accross listbox width. Maybe try to set listboxitem's HorizontalContentAlignment to Stretch :

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

这篇关于WPF中的列表框分隔符和省略最终分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆