Note that there are some explanatory texts on larger screens.

plurals
  1. POBind ListBox selected color to SelectedItem's property
    text
    copied!<p>I have a <code>ListBox</code> control bound to an <code>ObservableCollection&lt;ToDoCategory&gt;</code> as <code>ItemsSource="{Binding Categories}</code>. Inside the <code>ToDoCategory</code> there are <code>CategoryName</code> and <code>CategoryColor</code> (both strings). <code>CategoryName</code> is bound inside the <code>ItemTemplate</code>. Now, what I would like to do is to change the color of category selected in the <code>ListBox</code> based on <code>CategoryColor</code>. I already have string to brush <code>IValueConverter</code> which returns <code>SolidColorBrush</code> for string. It is also correctly defined in the page resources. I know I need to change <code>ItemContainerStyle</code>. Currently, I have something like this:</p> <pre><code>&lt;Style x:Key="CategoryListBoxContainerStyle" TargetType="ListBoxItem"&gt; Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ListBoxItem"&gt; &lt;Border...&gt; &lt;VisualStateManager.VisualStateGroups&gt; &lt;VisualState x:Name="Selected"&gt; &lt;Storyboard&gt; &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&gt; &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{Binding CategoryColor, Converter=StringToBrushConverter}"/&gt; &lt;/ObjectAnimationUsingKeyFrames&gt; &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&gt; &lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/&gt; &lt;/ObjectAnimationUsingKeyFrames&gt; &lt;/Storyboard&gt; &lt;/VisualState&gt; . . . &lt;/style&gt; </code></pre> <p>This doesn't work. How do I bind the Background of the InnerGrid to <code>CategoryColor</code> from the <code>ToDoCategory</code>?</p>
 

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