Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can style the <code>ListBoxItem</code> to trigger on the <code>IsSelected</code> property. Here's an example:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/ms750821(v=vs.85).aspx" rel="noreferrer">ListBoxItem ControlTemplate Example</a></li> </ul> <p>and then you use it like this:</p> <pre><code>&lt;ListBox ItemContainerStyle="{StaticResource yourListBoxItemStyle}"&gt; </code></pre> <p>or directly in the <code>ListBox</code> itself:</p> <pre><code>&lt;ListBox.ItemContainerStyle&gt; &lt;Style TargetType=”ListBoxItem”&gt; ... &lt;/Style&gt; &lt;/ListBox.ItemContainerStyle&gt; </code></pre> <p><strong>Edit:</strong></p> <p>Here is a complete example with both an <code>ItemTemplate</code> and an <code>ItemContainerStyle</code> that puts a semi-opaque layer on top of the selected item.</p> <pre><code>&lt;Grid&gt; &lt;Grid.Resources&gt; &lt;x:Array Type="sys:String" x:Key="sampleData"&gt; &lt;sys:String&gt;Red&lt;/sys:String&gt; &lt;sys:String&gt;Green&lt;/sys:String&gt; &lt;sys:String&gt;Blue&lt;/sys:String&gt; &lt;/x:Array&gt; &lt;DataTemplate x:Key="listBoxItem"&gt; &lt;Rectangle Fill="{Binding}" Width="100" Height="100"/&gt; &lt;/DataTemplate&gt; &lt;Style TargetType="ListBoxItem" x:Key="listBoxItemStyle"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ListBoxItem"&gt; &lt;Grid&gt; &lt;ContentPresenter /&gt; &lt;Rectangle x:Name="Rectangle" Fill="Black" Opacity="0"/&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsSelected" Value="true"&gt; &lt;Setter TargetName="Rectangle" Property="Opacity" Value="0.5"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Grid.Resources&gt; &lt;ListBox ItemsSource="{StaticResource sampleData}" ItemTemplate="{StaticResource listBoxItem}" ItemContainerStyle="{StaticResource listBoxItemStyle}" /&gt; &lt;/Grid&gt; </code></pre> <p><strong>Edit</strong></p> <p>After a comment, here is the version using a "unified" template:</p> <pre><code>&lt;Style TargetType="ListBoxItem" x:Key="listBoxItemStyle"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ListBoxItem"&gt; &lt;Grid&gt; &lt;Rectangle Name="Rectangle" Fill="{Binding}" Width="100" Height="100" Opacity="1"/&gt; &lt;/Grid&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsSelected" Value="true"&gt; &lt;Setter TargetName="Rectangle" Property="Opacity" Value="0.5"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload