c# - 网格和列表框有一些我无法摆脱的边框

标签 c# wpf xaml listbox styles

我是 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);
}

关于c# - 网格和列表框有一些我无法摆脱的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10839679/

相关文章:

c# - 使用 DataContractSerializer 序列化覆盖的属性

c# - 如何删除点后的所有数字?

c# linq 选择不同的具有多个属性的元素

c# - XPath 绑定(bind)以读取 wpf 中的 InnerXml

c# - 检查对 Web 服务的传入调用是否通过 Https

c# - 找不到端点元素

wpf - 处理 MVVM 架构中的依赖关系

wpf - 数据网格延伸出窗口

xaml - 如何在Xamarin iOS,Android中以Xamarin形式设计以下 GridView

c# - 如何增加单选按钮之间的间距