【发布时间】: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