簡體   English   中英

如何擺脫這種WPF / XAML ListBox填充?

[英]How do I get rid of this WPF/XAML ListBox Padding?

我在學習WPF時遇到了一些麻煩,我已經設置了一個自定義UserControl,如下所示:

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:LobbyApp" x:Class="LobbyApp.ChatPanel" 
         mc:Ignorable="d" 
         d:DesignHeight="200" d:DesignWidth="600">
<UserControl.Resources>
    <DataTemplate DataType="{x:Type local:ChatMessage}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition>
                    <ColumnDefinition.Width>Auto</ColumnDefinition.Width>
                    <ColumnDefinition.SharedSizeGroup>Date</ColumnDefinition.SharedSizeGroup>
                </ColumnDefinition>
                <ColumnDefinition>
                    <ColumnDefinition.Width>Auto</ColumnDefinition.Width>
                    <ColumnDefinition.SharedSizeGroup>Who</ColumnDefinition.SharedSizeGroup>
                </ColumnDefinition>
                <ColumnDefinition>
                    <ColumnDefinition.Width>*</ColumnDefinition.Width>
                </ColumnDefinition>
            </Grid.ColumnDefinitions>
            <DataGridCell BorderThickness="0" Content="{Binding Timestamp}" Grid.Column="0" Background="Black" Foreground="LightCyan"/>
            <DataGridCell BorderThickness="0" Content="{Binding Who}" Grid.Column="1" Background="Black" Foreground="LightBlue"/>
            <DataGridCell BorderThickness="0" Grid.Column="2" Background="Black" Foreground="White">
                <TextBox Text="{Binding What, Mode=OneWay}" IsReadOnly="True" TextWrapping="Wrap"/>
            </DataGridCell>
        </Grid>
    </DataTemplate>
</UserControl.Resources>

<ListBox ItemsSource="{Binding History}" SnapsToDevicePixels="True" Background="Magenta" Padding="0"/>

我認為大多數都無所謂,只是為了完整起見就包括在內。 有趣的是,我在滾動條外部看到了洋紅色的背景,就像列表框的內容一樣,它的滾動條實際上​​是填充在列表框內部的。 看起來像這樣:

列表框填充

即使列表框大小合適,我也可以看到洋紅色的洋紅色,這樣縮小時更容易看到。

我已經嘗試過在可以消除品紅色的每個元素上使用每個邊距/填充,但是我似乎做不到。 我不知道是什么原因造成的,或者如何“修復”它。 我可能會將示例簡化為最基本的部分,但我認為我會首先發布,因為這可能只是一個愚蠢的答案。 如果是這樣,我表示歉意。

這是因為ListBox的邊框填充的硬編碼值為“ 1”。

<ControlTemplate TargetType="{x:Type ListBox}">
                <Border Name="Bd"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        SnapsToDevicePixels="true"
                        Padding="1">
                    <ScrollViewer Padding="{TemplateBinding Padding}"
                                  Focusable="false">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>

因此,您可以自己修改模板,也可以像下面這樣修改它:

void OnListBoxLoaded(object sender, RoutedEventArgs e)
    {
        Border border = VisualTreeHelper.GetChild((ListBox)sender, 0) as Border;
        if(border != null)
        {
            border.Padding = new Thickness(0);
        }
    }

以上假設您正在使用默認的Windows aero主題。 如果更改主題,或者確實更改了航空主題,則可能會中斷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM