2014-02-07 42 views
0

enter image description here刪除ListBoxItem周圍的矩形

上面是我的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> 

然後項不可選擇了。

回答

1

在我看來,你所說的'矩形'只是ListBoxItem.Background。它也看起來ListBox控制中使用的默認選擇Color。如果是這樣,您可以通過在本地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> 
+0

這工作,謝謝。 – Gerard

+0

還值得注意的是,您可以使用這些相同的屬性來設置默認選擇的顏色。 – Sheridan

+0

這不適用於Windows 8. – pindumb

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

+0

哇...這是一個很簡單的任務很多工作。 – Sheridan

+0

此解決方案並不令人滿意,因爲listboxitem的選擇不再有效 - 比較我的問題,而只是嘗試controltemplate的'。 – Gerard

+0

你的意思是選擇ListBoxItem不再工作?請下載可能的示例應用程序a測試i。鏈接,你可以在我的答案找到。 – Jan