2011-11-16 75 views
2

我正在WPF應用程序上工作,我想在ListViewItem上作出樣式。我爲此做了一個風格,但這並不能給我想要的確切結果。除此之外,我希望CornerRadius在這種風格,所以我如何可以添加此?如何在ListView SelectedItem中創建樣式。

  <ListView x:Name="MenuBarList" 
        Grid.Row="1" 
        Height="{Binding MainMenuHeight}" 
        Width="{Binding MainMenuWidth}" 
        ItemsSource="{Binding Path=Menu.Options}" 
        SelectedItem="{Binding Path=SelectedMainMenuOption, Mode=TwoWay}" 
        Foreground="White" 
        Background="Transparent" 
        VerticalContentAlignment="Center" 
        HorizontalContentAlignment="Center" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" 
        IsSynchronizedWithCurrentItem="True" 
        IsTabStop="False"> 

       <ListView.Style> 
        <Style TargetType="{x:Type ListView}"> 
         <Setter Property="BorderBrush" Value="White"/> 
         <Setter Property="BorderThickness" Value="0"/> 
         <Setter Property="Margin" Value="0"/> 
         <Setter Property="Width" Value="300"/> 
         <Style.Resources> 
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#9449b0"/> 
          <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#9449b0"/> 
        </Style.Resources> 
        </Style> 
       </ListView.Style> 


       <ListView.ItemTemplate> 
        <DataTemplate> 

         <StackPanel Orientation="Horizontal" Focusable="False" VerticalAlignment="Center" HorizontalAlignment="Center"> 
          <Image Source="{Binding IconPath}" 
            Focusable="False" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center" 
            IsHitTestVisible="False"/> 

          <TextBlock Text="{Binding Title}" 
             Focusable="False" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Center" 
             FontFamily="Segoe UI" 
             FontSize="26" 
             IsHitTestVisible="False"/> 
         </StackPanel> 
       </DataTemplate> 

       </ListView.ItemTemplate> 
      </ListView> 

回答

9

快速解決方案是覆蓋項目模板。您可以參考有關contol樣式的WPF或Silverlight主題。查看ListViewItem完整模板的BureauBlue.xaml

<ListView x:Name="uiList"> 
    <ListView.Resources> 
     <ControlTemplate x:Key="SelectedTemplate" TargetType="ListViewItem"> 
      <Border SnapsToDevicePixels="true" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}" 
        Background="{TemplateBinding Background}" 
        CornerRadius="5" x:Name="border"> 
       <ContentControl 
        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
        Margin="2,2,2,2" 
        VerticalAlignment="Stretch" 
        Content="{TemplateBinding Content}" /> 
      </Border> 
     </ControlTemplate> 
     <Style TargetType="ListViewItem"> 
      <Style.Triggers> 
       <MultiTrigger> 
        <MultiTrigger.Conditions> 
         <Condition Property="IsSelected" Value="true" /> 
         <Condition Property="Selector.IsSelectionActive" Value="true" /> 
        </MultiTrigger.Conditions> 
        <Setter Property="Background" Value="Pink" /> 
        <Setter Property="Template" Value="{StaticResource SelectedTemplate}" /> 
       </MultiTrigger> 
      </Style.Triggers> 
     </Style> 
    </ListView.Resources> 
    <ListViewItem Content="10" /> 
    <ListViewItem Content="11" /> 
</ListView> 
+0

我希望的結果是我想要otSeletedItem將用粉紅色突出顯示,因此需要cornerRadius。 –

+0

我已將我的代碼放入XAML中,以便您可以查看並僅僅是我想要選擇的項目的圓角。 –

+1

不幸的是,這不適用於GridView –