2016-04-19 98 views
3

を訪れた後、私は次のコードを持っている:変更背景色

   <ItemsControl Grid.Row="1" ItemsSource="{Binding Activities}"> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <ctrls:AlignableWrapPanel MaxWidth="400" HorizontalContentAlignment="Center" 
                 HorizontalAlignment="Center" /> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
        <ItemsControl.ItemContainerStyle> 
         <Style TargetType="ContentPresenter"> 
          <Setter Property="HorizontalAlignment" Value="Center" /> 
         </Style> 
        </ItemsControl.ItemContainerStyle> 
       </ItemsControl> 

今、私はすでに項目についてのページにつながる(クリックされた各項目の背景色を持っていると思いますが) かわった。アイテムを再度クリックすると、背景色が元に戻ります。

どうすればいいですか?

私はすでにChange background color for selected ListBox itemを見ました。次のコードが含まれている答えがあります:私はすでにのContentPresenterのスタイルを設定しておりますので

 <Style TargetType="ListBoxItem"> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True" > 
        <Setter Property="FontWeight" Value="Bold" /> 
        <Setter Property="Background" Value="Transparent" /> 
        <Setter Property="Foreground" Value="Black" /> 
       </Trigger> 
      </Style.Triggers> 
      <Style.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
      </Style.Resources> 
     </Style> 

ああ、私は簡単に自分のコードにこのコードを挿入することはできません。それに、それはおそらく私が望むものとまったく同じではありません。

答えて

0

これらのスタイルを組み合わせることができます。しかし、キャッチがあります:SystemColors.HighlighBrushを無効にすると、Windows 8以降では役に立ちません。さらに、ItemsControlでは利用できないマルチセレクションを有効にする必要がありますが、ListBoxにはそれがあります。このコードを試してみてください。

<ListBox ItemsSource="{Binding Activities}" SelectionMode="Multiple"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <ctrls:AlignableWrapPanel MaxWidth="400" HorizontalContentAlignment="Center" HorizontalAlignment="Center" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border> 
       <ContentPresenter Content="{Binding}"/> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <ItemsControl.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="HorizontalAlignment" Value="Center" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <Border Background="{TemplateBinding Background}"> 
          <ContentPresenter Content="{TemplateBinding Content}"/> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True" > 
        <Setter Property="FontWeight" Value="Bold" /> 
        <Setter Property="Background" Value="Yellow" /> 
        <Setter Property="Foreground" Value="Black" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
</ListBox> 
0

コードは、トリックを行うこと:

<ItemsControl ...> 
    ... 
    <ItemsControl.Resources> 
     <Style TargetType="ToggleButton"> 
      <Style.Triggers> 
       <Trigger Property="IsChecked" Value="True"> 
        <Setter Property="Background" Value="Green" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style>     
    </ItemsControl.Resources> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <ToggleButton Content="{Binding}"> 
       <ToggleButton.Template> 
        <ControlTemplate> 
         <Label Content="{Binding}" Background="{TemplateBinding Background}" /> 
        </ControlTemplate> 
       </ToggleButton.Template> 
      </ToggleButton> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
0

は商品(「活動」の項目タイプ)

ののviewmodelに「IsVisited」のようなプロパティを追加することができます

アイテムをクリックするたびに、このプロパティをtrue/falseに設定します。詳細ページを開くのと同じ方法で実行します。

次に、アイテムのDataTemplateで、 "IsVisited"プロパティにバインドし、アイテムのパネルの背景色を設定するDatatriggerを作成できます。

さらに詳しいヘルプについては、コードを詳しく記入してください。

関連する問題