2011-02-23 165 views
1

我有幾個關於樣式ListView的問題。例如,我有一些風格:樣式ListView(自定義模板)WPF

<Style x:Key="MyListView" TargetType="{x:Type ListView}"> 
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
    <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
    <Setter Property="VerticalContentAlignment" Value="Center"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListView}"> 
       <Border x:Name="PART_ControlBorder" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="0"> 
        <Grid> 
         <ScrollViewer Grid.Row="1" 
          VerticalScrollBarVisibility="Hidden" 
          HorizontalScrollBarVisibility="Hidden" 
          CanKeyboardScroll="False" 
          Padding="{TemplateBinding Padding}" 
          Focusable="false"> 
          <ItemsPresenter x:Name="PART_ItemsPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
         </ScrollViewer> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="ItemTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <Border x:Name="ItemBorder" CornerRadius="4" SnapsToDevicePixels="true" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="2" Padding="1"> 
        <Grid> 
         <TextBlock Text="{Binding Key}" Background="LightGray"/> 
        </Grid> 
       </Border> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 

    <Setter Property="ItemContainerStyle"> 
     <Setter.Value> 
      <Style TargetType="{x:Type ListViewItem}"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
       <Style.Triggers> 
        <Trigger Property="IsSelected" Value="True"> 
         <Setter Property="Background" Value="{x:Null}" /> 
         <Setter Property="BorderBrush" Value="{x:Null}" /> 
         <Setter Property="Foreground" Value="Black" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Setter.Value> 
    </Setter> 
</Style> 
  1. 爲什麼不工作的研究背景和BorderBrush制定者(ItemContainerStyle)?我不得不隱藏使用重新定義爲系統刷選擇,但它的錯誤的方式:

    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/> 
    

  2. 我怎麼能設置爲BorderBrush ItemBorder(鼠標懸停事件)?

  3. 我如何設置BorderBrush爲ItemBorder(僅適用於所選項目)?

  4. 我該如何改變默認選擇風格?

回答

1

我自己找到答案。 要爲ItemBorder設置樣式我不得不創建幾個DataTriggers並付諸的DataTemplate:

    <DataTemplate.Triggers> 
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListViewItem}},Path=IsSelected}" Value="True"> 
         <Setter TargetName="ItemBorder" Property="BorderBrush" Value="Lime"/> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListViewItem}}, Path=IsMouseOver}" Value="True"> 
         <Setter TargetName="ItemBorder" Property="BorderBrush" Value="Orange"/> 
        </DataTrigger> 
       </DataTemplate.Triggers> 

關於問題1和4我仍然沒有回答... ...(如果我不會算方法來改變默認系統畫筆)