【问题标题】:Grid and Listbox have some Border i cannot get rid of网格和列表框有一些我无法摆脱的边框
【发布时间】:2012-06-06 01:39:31
【问题描述】:

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

希望有人能帮我摆脱这个边界。非常感谢!

【问题讨论】:

  • 可以发一张外观截图吗?您将在代表 TextBox 边框的每个项目之间看到一条细线。

标签: c# wpf xaml listbox styles


【解决方案1】:

您是否尝试过为 ListBox 或 Grid 设置 ControlTemplate。您应该可以通过编写自己的模板来摆脱边界。

【讨论】:

  • 谢谢,我试试看!
  • 列表框不需要的“边框”实际上是 1 个像素,在框周围。
【解决方案2】:

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

【讨论】:

  • 我现在尝试设置 ControlTemplate,但我无法弄清楚。你有我的链接,也许它解释了该怎么做?
  • 我会为您上传一个示例,只需几分钟。
  • 在此处上传了如何更改Template 的示例:dl.dropbox.com/u/39657172/ListBoxTemplateExample.zip
  • 太棒了!非常感谢,非常感谢!
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多