删除ListBoxItem周围的矩形

时间:2014-02-07 12:56:18

标签: wpf listboxitem

enter image description here

上图是我的listboxitem图片 我想知道是否有可能移除椭圆形的矩形 Padding和BorderThicknes已经为0了。

我使用这种造型:

<ListBox.ItemContainerStyle>
   <Style TargetType="ListBoxItem"> ...
   <Style.Triggers>
        <DataTrigger Binding="{Binding Data}" Value="ellipse">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Grid>
                            <Ellipse Fill="{Binding Brush}" Stroke="Black"/>
                             <TextBlock Text="{Binding Int}" />
                        </Grid>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>

我试过了:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
            <ContentPresenter />
        </ControlTemplate>
    </Setter.Value>
</Setter>

然后这些项目不再可供选择。

2 个答案:

答案 0 :(得分:6)

You should create control template for ListBoxItem.然后你可以改变ListBoxItem的背景。

  <Window.Resources>
    <Style x:Key="{x:Type ListBoxItem}"
           TargetType="ListBoxItem">
        <Setter Property="SnapsToDevicePixels"
                Value="true" />
        <Setter Property="OverridesDefaultStyle"
                Value="true" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="Border"
                            BorderThickness="2"
                            Padding="2"
                            SnapsToDevicePixels="true">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected"
                                 Value="true">
                            <Setter TargetName="Border"
                                    Property="Background"
                                    Value="{x:Static Brushes.Transparent}" />

                            <Setter TargetName="Border"
                                    Property="BorderBrush"
                                    Value="{x:Static Brushes.WhiteSmoke}" />
                        </Trigger>
                        <Trigger Property="IsEnabled"
                                 Value="false">
                            <Setter Property="Foreground"
                                    Value="{x:Static Brushes.Transparent}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox Margin="5" ItemsSource="{Binding Path=Datas}">
       <ListBox.ItemTemplate>
           <DataTemplate>
               <Grid>
                    <Ellipse Width="30"
                             Height="30"
                             Fill="{Binding Path=EllipseBrush}"/>
                   <TextBlock Text="{Binding Path=Number}"/>
               </Grid>
           </DataTemplate>
       </ListBox.ItemTemplate>
    </ListBox>
</Grid>

您可以下载here

的示例

答案 1 :(得分:1)

在我看来,你所谈论的'矩形'只是ListBoxItem.Background。它也看起来,就像Color控件中使用的默认选择ListBox一样。如果是这样,您可以通过在本地Resources部分中设置一些属性值来轻松隐藏它。请尝试将这些内容添加到ListBox.Resources部分:

<Style.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>