2012-05-31 55 views
3

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

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

+0

你能后的截图外观?您将在代表TextBox边框的每个项目之间看到一条细线。 –

回答

6

的问题是内部的ListBoxTemplate第一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。

要么改变TemplateListBox或者在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); 
} 
+0

你是对的!非常感谢你! –

+0

当然,很乐意帮助:) –

+0

我试图设置ControlTemplate现在,但我无法弄清楚。你有链接给我,也许在哪里解释了该怎么做? –

1

您是否尝试过为列表框或网格设置ControlTemplate?你应该能够通过编写自己的模板来摆脱边界。

+0

谢谢,我会试试这个! –

+0

列表框中不需要的“边框”实际上是1个像素。 –