2016-07-01 18 views
4

私はここで助けが必要です。なぜ私が見つけた解決策のどれも私の場合には役に立たないのです。ホバー上当初、私はデフォルトのライトブルーの中のTextBlockのハイライトを見て、各行をListViewItemの背景色をマウスオーバーで変更します

<ListView.Items> 
    <ListViewItem> 
      <TextBlock xml:space="preserve"> 1 <Bold>I'm bold</Bold> </TextBlock> 
    </ListViewItem> 
    <ListViewItem> 
      <TextBlock xml:space="preserve"> 2 Im not </TextBlock> 
    </ListViewItem> 
</ListView.Items> 

:さんは、これらの項目とリストビューを考えてみましょう。それはテキストのみでエリア下線:

を私は行全体の1欲しいというのハイライトをしたくない、と私は色を決定します。 enter image description here

私は、スタイル、トリガー、またはItemContainerStyleで遊んでいました。 Textboxの背景と、テキスト付きの領域のListViewItemのいずれかを考慮する必要があることに気付きました。全体の行の背景は、ListView.ItemContainerStyleのビジネスのようです。

のTextBox FOTスタイルを加算した結果:

<Style x:Key="TextBlockStyle" TargetType="{x:Type TextBlock}"> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Foreground" Value="Black" /> 
       <Setter Property="Background" Value="White"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

<ListView Grid.Column="1" Margin="0" HorizontalContentAlignment="Stretch" BorderThickness="0" > 
     <ListView.Resources> 
       <Style BasedOn="{StaticResource TextBlockStyle}" TargetType="{x:Type TextBlock}" /> 
      </ListView.Resources> 

は次のとおりです。enter image description here

その後、私はテキストボックスの下にリストビューの背景を取り除くためにしようとする別のスタイルを追加します。

<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}"> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Background" Value="Gold" /> 
         </Trigger> 
        </Style.Triggers> 
    </Style> 

<ListView Grid.Column="1" Margin="0" HorizontalContentAlignment="Stretch" BorderThickness="0" > 
     <ListView.Resources> 
       <Style BasedOn="{StaticResource TextBlockStyle}" TargetType="{x:Type TextBlock}" /> 
       <Style BasedOn="{StaticResource ListViewItemStyle}" TargetType="{x:Type ListViewItem}" />  
      </ListView.Resources> 

しかし、これはまったく効果がありません。

そして、これで行全体を強調しようとしているが動作しません:

<ItemsControl.ItemContainerStyle> 
    <Style> 
     <Style.Triggers> 
       <Trigger Property="Control.IsMouseOver" Value="True"> 
        <Setter Property="Control.Background" Value="Gold" /> 
         </Trigger> 
     </Style.Triggers> 
    </Style> 
</ItemsControl.ItemContainerStyle> 

そして時間のためのいくつかの他の提案を試してみました。この1つ: Remove the mouse over effect on a ListView in WPFは、TextBoxとListViewItemの両方のホバー上のテキストを強調表示しませんが、行全体の背景を変更する方法はわかりません。 誰かが私がやろうとしていることの例を教えてもらえますか?

+0

DataTemplate内のコンテナの背景を変更します。 – AnjumSKhan

答えて

7

参照して指定された要素のすべてのスタイル・オプションを変更する最も簡単な方法は、コントロールのデフォルトのテンプレートをエクスポートすることです。

Therefore open Visual Studio or Blend and Right Click in the Design View on a control. Hover to 'Edit Template' -> And select 'Edit a Copy...' 出力:

 <SolidColorBrush x:Key="Item.MouseOver.Background" Color="Gold"/> 
     <SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/> 
     <SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/> 
     <SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/> 
     <SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/> 
     <SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/> 

     <Style x:Key="ListViewContainerStyle" TargetType="{x:Type ListViewItem}"> 
      <Setter Property="SnapsToDevicePixels" Value="True"/> 
      <Setter Property="Padding" Value="4,1"/> 
      <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
      <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="BorderBrush" Value="Transparent"/> 
      <Setter Property="BorderThickness" Value="1"/> 
      <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListViewItem}"> 
         <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> 
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Border> 
         <ControlTemplate.Triggers> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="IsMouseOver" Value="True"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/> 
           <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/> 
          </MultiTrigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="Selector.IsSelectionActive" Value="False"/> 
            <Condition Property="IsSelected" Value="True"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/> 
           <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/> 
          </MultiTrigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="Selector.IsSelectionActive" Value="True"/> 
            <Condition Property="IsSelected" Value="True"/> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/> 
           <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/> 
          </MultiTrigger> 
          <Trigger Property="IsEnabled" Value="False"> 
           <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

今すぐあなたのItemContainerStyleをカスタマイズし得るための良い出発点を持っています。

+0

このコードは、私が提供したリンクのソリューションの考え方に従います。ここでの結果は初期の状況と同じですが、カスタマイズされた色です。 ListViewItemの幅を行全体と同じにすることができれば、私の問題は解決します。しかし、Horizo​​ntalContentAlignmentプロパティを変更して、コードで提供されている値ではなく、ストレッチ値を使用すると、機能しません。 –

8

これを試してみてください:

 <ListView.ItemContainerStyle> 
      <Style TargetType="{x:Type ListViewItem}"> 
       <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Background" Value="Gold" /> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </ListView.ItemContainerStyle> 
+0

私はこれを試しましたが、これは行全体ではなくテキストで領域を強調しただけです。 –

+1

'TargetType =" {x:Type ListViewItem} "はこの場合重要です。 – mechanic

+0

その方法は機能しません。上記の@mathaykの記事を参照してください。リストビューははるかに複雑なコントロールと変化するバックグラウンドは何もしません。 – Brun

0

まあ、私はあきらめて別の道を試みました。私はButtonViewのListViewItemsを変更すると、マウス全体で行全体が強調表示されていることに気付きました。私は審美的な理由だけでなく、これを探していました。元の状況では、ユーザはポインタをテキストの上に移動して選択する必要があります。しかし、今では、その行にポインタを置くだけで、テキストの上にマウスを移動する必要はありません。私はそれをユーザーにとってはより快適で素早く見つけることができます。これはインターフェイスの種類にとって非常に重要です。なぜなら、これはかなり頻繁に行う必要があるからです。

したがって、@ mathaykのコードはListViewItemsではなくButtonsで動作します。

お時間をいただきありがとうございます。

関連する問題