在列表框中,当我选择一个项目时,我仍会在listboxitem周围获得某种边框或空格,从而创建您在图像中看到的线条。我想删除那条线,我绕了一圈。
顺便说一下,它已经HorizontalContentAlignment = "Stretch"
。
我做错了什么?
答案 0 :(得分:2)
我认为这是ListBox中的一个错误。这是我如何伸展项目模板以填充ListBox的宽度。使每个列表项的容器成为单格网格,并使用网格加载事件来调用将网格拉伸到ListBox宽度的方法。
<ListBox x:Name="lvHolePatterns" ItemsSource="{Binding HolePatterns}"
SelectedItem="{Binding ActivePattern, Mode=TwoWay}"
HorizontalContentAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Visible"
Background="Gray">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Loaded="StretchFrameworkElement">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Border Margin="0,3,5,3" BorderThickness="1" BorderBrush="SlateGray" CornerRadius="4"
RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Stretch" >
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset=" 1"/>
<GradientStop Color="#FF396DBE" Offset="0"/>
</LinearGradientBrush>
</Border.Background>
<StackPanel Orientation="Vertical" >
<TextBlock FontWeight="Bold" Text="{Binding Path=PatternName}" Foreground="WHITE" VerticalAlignment="Center" Margin="5,5,0,5"/>
<StackPanel Orientation="Horizontal" Margin="5,0,0,5">
<TextBlock Text="{Binding Path=HoleCount}" Margin="10,0,0,0" Foreground="WHITE" VerticalAlignment="Center"/>
<TextBlock Text=" Holes" Margin="3,0,0,0" Foreground="WHITE" VerticalAlignment="Center"/>
<CheckBox Content="Visible" IsChecked="{Binding Visible, Mode=TwoWay}" Foreground="WHITE" Margin="10,0,0,0" />
</StackPanel>
</StackPanel>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
然后,添加此方法来处理网格加载事件:
private void StretchFrameworkElement(object sender, RoutedEventArgs e)
{
// found this method at: http://silverlight.net/forums/p/18918/70469.aspx#70469
FrameworkElement t = sender as FrameworkElement;
ContentPresenter p = VisualTreeHelper.GetParent(t) as ContentPresenter;
p.HorizontalAlignment = HorizontalAlignment.Stretch;
}
答案 1 :(得分:2)
解决此问题的最佳方法是重新定义模板,如下所示:
<ListBox>
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border
SnapsToDevicePixels="true"
x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="0"> <!-- This was originally 1 -->
<ScrollViewer
Focusable="false"
Padding="{TemplateBinding Padding}">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter
Property="Background"
TargetName="Bd"
Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter
Property="ScrollViewer.CanContentScroll"
Value="false"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ListBox.Template>
<ListBoxItem>Item1</ListBoxItem>
<ListBoxItem>Item2</ListBoxItem>
<ListBoxItem>Item3</ListBoxItem>
</ListBox>
答案 2 :(得分:1)
这是我的应用程序的WPF版本中的ListView。 ListView DataTemplate使用Border,它自动延伸到ListView的宽度。没有像上面的Silverlight答案那样需要额外的方法。
<ListView x:Name="lvHolePatterns" ItemsSource="{Binding Path=HolePatterns}"
SelectedItem="{Binding Path=ActivePattern}"
IsSynchronizedWithCurrentItem="True"
HorizontalContentAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Visible"
Background="Gray">
<ListView.ItemContainerStyle>
<Style BasedOn="{StaticResource lvRowHighlighter}" TargetType="{x:Type ListViewItem}">
<!--<Setter Property="Height" Value="50" />-->
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Border Margin="0,3,5,3" BorderThickness="1" BorderBrush="SlateGray" CornerRadius="4" RenderTransformOrigin="0.5,0.5">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF000000" Offset=" 1"/>
<GradientStop Color="#FF396DBE" Offset="0"/>
</LinearGradientBrush>
</Border.Background>
<StackPanel Orientation="Vertical">
<TextBlock FontWeight="Bold" Text="{Binding Path=PatternName}" Foreground="WHITE" VerticalAlignment="Center" Margin="5,5,0,5"/>
<StackPanel Orientation="Horizontal" Margin="5,0,0,5">
<TextBlock Text="{Binding Path=HoleCount}" Margin="15,0,0,0" Foreground="WHITE" VerticalAlignment="Center"/>
<TextBlock Text=" Holes" Margin="3,0,0,0" Foreground="WHITE" VerticalAlignment="Center"/>
<CheckBox Content="Visible" IsChecked="{Binding Path=Visible}" Foreground="WHITE" Margin="10,0,0,0" />
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
答案 3 :(得分:0)
只需将其添加到文本框实例属性:
HorizontalContentAlignment="Stretch"
如果没有任何代码可以做到这一点。
吉利
答案 4 :(得分:0)
我找到了列表框项中硬编码填充问题的一个非常简单的解决方法:将datatemplate中最外层元素的边距设置为-1
- 赫农