簡體   English   中英

網格和列表框有一些我無法擺脫的邊框

[英]Grid and Listbox have some Border i cannot get rid of

我對WPF很新。 我只是嘗試使用Grid和Listbox進行一些布局,但是我有一些填充/間距/邊距/邊框(簡稱為邊框),我無法逃脫。

邊界圍繞四個元素,元素本身沒有問題,它們之間沒有空間。

還嘗試了WPF Inspector,但我無法找到它的來源。 沒有什么可看的。

這是我的XAML:

<Window x:Class="WpfElements.FourElements"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="FourElements" Height="701" Width="351">
<Grid Background="Red" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <ListBox Margin="0" BorderThickness="0" Padding="0" Grid.Row="0" Grid.Column="0" Background="Lime" SelectionMode="Single" ItemsSource="{Binding Path=Texts}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding ItemText}" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Col}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0"></TextBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Column" Value="{Binding Path=Col}"/>
                <Setter Property="Grid.Row" Value="{Binding Path=Row}"/>
                <Setter Property="ContentControl.HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="ContentControl.VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="ContentControl.Margin" Value="0"/>
                <Setter Property="ContentControl.Padding" Value="0"/>
                <Setter Property="Control.BorderThickness" Value="0"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

希望有人可以幫助我擺脫這個邊界。 非常感謝!

問題是ListBox Template的第一個Border ,它的Padding設置為1,1,1,1
看起來像這樣

<Style TargetType="{x:Type ListBox}" ...>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Border x:Name="Bd"
                        Padding="1">
                <!-- ... -->

正如您在Template看到的, Border名稱是Bd。

更改ListBoxTemplate或在Loaded事件中將其設置為0

XAML

<ListBox ...
         Loaded="ListBox_Loaded">

代碼背后

private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
    ListBox listBox = sender as ListBox;
    Border Bd = listBox.Template.FindName("Bd", listBox) as Border;
    Bd.Padding = new Thickness(0);
}

您是否嘗試過為ListBox或Grid設置ControlTemplate。 您應該能夠通過編寫自己的模板來擺脫邊界。

暫無
暫無

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

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