2017-08-24 3 views
0

いくつかの要素が選択され、マウスがその要素を乗り越えるときに、要素の色を変更しようとしています。私の最初の試みでは、ItemsControlと境界線を使って色の変更を有効にしていますが、これはマウスの色の変化を私に与えていますが、ここに示すようにIsFocussedを選択するためにどのプロパティをトリガーする必要があるかはわかりません正しくありません:ボーダを選択することはできますか?または、リストボックスの継承を解除する方法を教えてください。

<UserControl x:Class="Crp.CodePlusTeamExplorerSectionView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:Crp.ViewModels="clr-namespace:Crp.ViewModels" 
     mc:Ignorable="d" 
     d:DesignHeight="250" d:DesignWidth="300"> 
<UserControl.DataContext> 
    <Crp.ViewModels:RelatedViewsViewModel/> 
</UserControl.DataContext> 
<Control.Resources> 
    <Style x:Key="styleWithTrigger" TargetType="Border"> 
     <Setter Property="Background" Value="Transparent" /> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Background" Value="#3E3E40" /> 
      </Trigger> 
      <Trigger Property="IsFocused" Value="True"> 
       <Setter Property="Background" Value="#007ACC" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Control.Resources> 
<ItemsControl Name="RelatedViewsICtl" ItemsSource="{Binding RelatedViews}" MouseDoubleClick="RelatedViewsLB_MouseDoubleClick"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border Name="border" VerticalAlignment="Stretch" Height="23" MinHeight="22" Style="{StaticResource styleWithTrigger}"> 
       <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"> 
        <TextBlock Text="" Margin="1"/> 
        <Image Source="Resources\Review.png"/> 
        <TextBlock Text="{Binding Path=Id}" Margin="1"/> 
        <TextBlock Text="-" Margin="1"/> 
        <TextBlock Text="{Binding Path=Name}" Margin="1" /> 
       </StackPanel> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Vertical"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 

私が試した他の選択肢はより良いアプローチのように思えるが、リストボックスが正しいプロパティを継承から以内に私の要素を停止し、私はどのようにわからないよListBox、これを変換することですこれを修正する

+0

その後、ListBoxItemコンテナのスタイルを設定することができます。 "リストボックスは、正しいプロパティを継承することから自分の要素を停止させる" - このステートメントを明確にして詳細を教えてください。 – ASh

+0

システムカラーをオーバーライドすることもできます。それは私が本当に好きなのは、世界中のものをやりたいときです。トリガーを使用したり、イベントを心配する必要がなくなりました。特定のObjectのSystemColorを設定するだけで、 "MouseOver"が目的の色になります。 [参考](https://blogs.msdn.microsoft.com/wpf/2010/11/30/systemcolors-reference/) –

答えて

1

項目を選択できるようにするには、ListBoxを使用する必要があります。 ListBoxは、ItemsControlであり、基本的にはアイテムを選択する機能が追加されています。より理にかなってアウト・オブ・ボックスの選択をサポートしてリストボックスを使用して

<ListBox x:Name="RelatedViewsICtl" ItemsSource="{Binding RelatedViews}" MouseDoubleClick="RelatedViewsLB_MouseDoubleClick"> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="Background" Value="Transparent" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <Border Name="border" VerticalAlignment="Stretch" Height="23" MinHeight="22" 
             Background="{TemplateBinding Background}"> 
          <ContentPresenter /> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Background" Value="#3E3E40" /> 
       </Trigger> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="Background" Value="#007ACC" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"> 
       <TextBlock Text="" Margin="1"/> 
       <Image Source="Resources\Review.png"/> 
       <TextBlock Text="{Binding Path=Id}" Margin="1"/> 
       <TextBlock Text="-" Margin="1"/> 
       <TextBlock Text="{Binding Path=Name}" Margin="1" /> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <VirtualizingStackPanel Orientation="Vertical"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ListBox> 
+0

これはバックグラウンドで動作しましたが、ブラシを削除するために境界を調整する必要がありました。フォアグラウンド(テキストの色)私は手動で設定しなければならなかった。私はそれが正しい色を継承する方法を見ることができませんでした。 – Noel

+0

@Noel:有益な回答を投票することを忘れないでください:) – mm8

0

あなたは間違いなくリストボックスを使うべきですが、デフォルトのListBoxItemテンプレートが定義されている方法あなたがバックグラウンド値に設定したものを乗せてください。これはおそらくあなたのリストボックスの問題の理由です。説明はthis answerにあります。

つまり、ItemContainerStyle内のListBoxItemの背景プロパティに設定したものは、デフォルトのテンプレートトリガーがコントロール自体のプロパティではなく、そのテンプレートの特定の境界線をターゲットにしているため、使用されません。解決策は、のテンプレート全体をオーバーライドすることです。です。

XAMLエディタで入力して、それを選択して[プロパティ]タブに移動すると、すぐに取得できます。そこから、[その他] - > [テンプレート]の下で"Convert to a new resource"を選択できます。

関連する問題