0

I have a simple ComboBox inside a MVVM app.
The important things inside the ComboBox look like this:

    <ComboBox Name="EmployeesComboBox"
              ItemsSource="{Binding EmployeeEntries}"
              SelectedValuePath="Id"
              SelectedValue="{Binding Id}"
              Width="192" FontFamily="Arial" FontSize="12">
        <ComboBox.Resources>
            <Storyboard x:Key="BlinkingStoryboard">
                <ColorAnimation Storyboard.TargetProperty="Background.Color" Duration="0:0:2"
                        From="White" To="Yellow" RepeatBehavior="Forever" AutoReverse="True" />
            </Storyboard>
        </ComboBox.Resources>
    </ComboBox>

I changed even the style of the XAML of the ToggleButton(inside the ComboBox Style definitions) like follows:

<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Border x:Name="templateRoot" 
                        BorderBrush="{StaticResource ComboBox.Static.Border}"
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                    <Border x:Name="splitBorder"
                            BorderBrush="Transparent" BorderThickness="1" 
                            HorizontalAlignment="Right" Margin="0"
                            SnapsToDevicePixels="true" 
                            Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                        <Path x:Name="arrow" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z" 
                              Fill="{StaticResource ComboBox.Static.Glyph}"
                              HorizontalAlignment="Center" Margin="0" 
                              VerticalAlignment="Center"/>
                    </Border>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

(I took most of the definition out of the original style and only changed the Background="{StaticResource ComboBox.Static.Background}" to the Background="{TemplateBinding Background}".
After that I could use the Background property inside the XAML to change the color of the ComboBox as intended, but I need to fire the StoryBoard, so that I can make the ComboBox 'blink' by programmatically means.
As soon as I start the Storyboard the app crashes (timer dont work anymore, and so on) wihtout error messages.
Anyone an idea what I am missing?

Marcel Grüger
  • 885
  • 1
  • 9
  • 25

1 Answers1

0

You need to get a reference to the storyboard in code so you can start it. See Call a storyboard declared in xaml from c# (of course yours is defined in the combobox's resources so you'd need to get it from there).

Edit: to get more information about failures, enable trace settings for WPF in visual studio's settings - debugging/output window, then look in the output window for any warnings/errors.

Community
  • 1
  • 1
Alex Paven
  • 5,539
  • 2
  • 21
  • 35
  • From the code behind I call the Storyboard and it is found, but it doesn't work. I use a simple `Storyboard animationStoryboard = (Storyboard) EmployeesComboBox.FindResource("BlinkingStoryboard");` and then `animationStoryboard.Begin();` – Marcel Grüger May 08 '17 at 10:30
  • 1
    You'll need to get more information then; off the top of my head, enable trace settings for WPF in visual studio's settings - debugging/output window (or if already enabled, look in the output window for any warnings/errors). – Alex Paven May 08 '17 at 10:35
  • Ok, now I got the exception that there was no target for the animation. I set it with Storyboard.TargetName="ExployeesComboBox" and now it works fine. Thanks. Please change your text of your answer to that of your comment and I can call it a solution ;) – Marcel Grüger May 08 '17 at 11:04