在列表框中,当我选择一个项目时,我仍然会在我的列表框项目周围获得某种边框或空间,以创建您在图像中看到的行。我想去掉我用圆圈圈住的那条线。
顺便说一句,它已经有HorizontalContentAlignment = "Stretch"
了。
我做错了什么?
问题是:
发布于 2009-03-04 19:40:36
Matt是对的,因为这是一种bug。填充是在ListBox模板中硬编码的,而不是绑定到您可以更改的属性。
解决此问题的最好方法是重新定义模板,如下所示:
<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>
发布于 2009-03-04 19:26:25
我想这是ListBox中的一个bug。下面是我如何伸展项目模板以填充ListBox的宽度。使每个列表项的容器成为一个单元格网格,并使用grid loaded事件调用一个方法,该方法将网格扩展到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;
}
发布于 2009-03-04 20:40:13
这是我的应用程序的WPF版本中的ListView。ListView DataTemplate使用边框,它会自动延伸到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>
https://stackoverflow.com/questions/613464
复制