I have a ListBoxItemTemplate with VisualStateManager in XAML. The arrangement are 3 rows with 2 columns per row. I need to assign 2 mouse events (hover over and selected) to each box with images.
However, I only able to bind to 1st box and it doesn't feed other images when mouse over to other boxes.
<Grid>
...
<Image Grid.Row="0" Grid.Column="0" x:Name="Initial1" Height="53" Width="53" Source="/mine.Images;component/Images/a.png" Stretch="Fill" HorizontalAlignment="Left" Margin="28"
Opacity="0"/>
<Image Grid.Row="0" Grid.Column="0" x:Name="MouseOver1" Height="53" Width="53" Source="/mine.Images;component/Images/b.png" Stretch="Fill" HorizontalAlignment="Left" Margin="28"
Opacity="0"/>
</Grid>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" TargetName="MouseOver1" Value="1" />
<Setter Property="Opacity" TargetName="Initial1" Value="0" />...
Do I miss out anything? Please help me as I am new to WPF!
As requested by Naz_Firdouse, here is the code. Thanks.
<ControlTemplate x:Key="ListBoxItemTemplate" TargetType="{x:Type ListBoxItem}">
<Grid x:Name="grid" Height="Auto" Width="Auto" removed="{StaticResource SolidBrush0093B9F9}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard> <!--currently assign image for mouse over, when mouse over is triggered, the initial image will collapse and vice versa-->
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="mouseOverImage">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="initialImage">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="Focused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<!--these are the images assigned for initial and mouse over states-->
<Image x:Name="initialImage" Margin="0" Source="/mine.Images;component/Images/a.png" Stretch="Fill" />
<Image x:Name="mouseOverImage" Margin="0" Source="/mine.Images;component/Images/b.png" Stretch="Fill" Visibility="Collapsed"/>
<Style x:Key="ListBoxShadowEffect" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Height="Auto" Width="Auto" Margin="20,10,20,10">
<ListBoxItem Content="ListBoxItem" HorizontalAlignment="Left" Template="{StaticResource ListBoxItemTemplate}"/>
Currently I only tried out for 1st row 1st column in the ListBoxItem. However, it only showed initialImage and do not change to mouseOverImage when I do mouse over on it.