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

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

问题描述

我有两件事想要实现:

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

我目前的布局如下图:

这显示了 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>

更新

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

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 的 Horizo​​ntalContentAlignment 设置为 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中的列表框分隔符和最终分隔符的省略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)