I've got a set of StackPanels. They are named "Item0", "Item1", ..., "Item5".I'd like to change the Background when mouse over, change back when mouse leave, and keep the highlight color when an item is clicked.I tried <Style> <Trigger>, it works when mouse over.<Style x:Key="ItemStackPanel" TargetType="{x:Type StackPanel}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource ResourceKey=HoverItemBrush}"/>
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
Then I add handlers on each StackPanel to handle MouseLeftButtonDown, to make the background permanently changed.
It works too. but after one item is clicked, the mouseover effect doesn't work.this.Item0.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item1.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item2.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item3.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item4.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item5.MouseLeftButtonDown += Item_MouseLeftButtonDown;
What should I do?
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource ResourceKey=HoverItemBrush}"/>
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
Then I add handlers on each StackPanel to handle MouseLeftButtonDown, to make the background permanently changed.
It works too. but after one item is clicked, the mouseover effect doesn't work.this.Item0.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item1.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item2.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item3.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item4.MouseLeftButtonDown += Item_MouseLeftButtonDown;
this.Item5.MouseLeftButtonDown += Item_MouseLeftButtonDown;
What should I do?
解决方案 »
- 关于存储过程返回值的问题
- 怎么把Word中的数据写到C#的一个对象中去啊
- 求助,有关Socket监听DTU设备的问题
- DataGridView 编辑单元格
- Hashtable 中添加对象时,会随着对象值变化而变化,如何保证原先已赋值的Hashtable 不会被改动
- .net 怎样将Word中的表格或公式保存为图片~~~~急!!
- 急!主窗体对子窗体进行切换的问题
- 主线程等待子线程结果过程中,如何即处于等待状态,又不阻塞主线程继续响应其他消息?请高手支招,在线等..........
- 我用c#建立了虚拟目录,可是在IIS里面看不见我所建立的虚拟目录?????
- c#可作为脚本语言嵌入asp.net中么有没有人试过,我怎么运行不了????
- 从text读内容存到二维数组使用的问题
- |ZYCWPF| 为什么Style.DataTragger中的Setter无法设置Text属性,是不是这个只能设置样式,而数据字段不可以设置呢?
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Window.Resources>
<Style x:Key="StackPanelStyle1" TargetType="{x:Type StackPanel}"/>
<Storyboard x:Key="OnMouseEnter1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="stackPanel">
<EasingColorKeyFrame KeyTime="0" Value="Red"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="OnMouseLeave1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="stackPanel">
<EasingColorKeyFrame KeyTime="0" Value="Black"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
<Window.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="stackPanel">
<BeginStoryboard Storyboard="{StaticResource OnMouseEnter1}"/>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="stackPanel">
<BeginStoryboard x:Name="OnMouseLeave1_BeginStoryboard" Storyboard="{StaticResource OnMouseLeave1}"/>
</EventTrigger>
</Window.Triggers> <Grid x:Name="LayoutRoot">
<StackPanel x:Name="stackPanel" HorizontalAlignment="Left" Height="60" Margin="105,129,0,0" VerticalAlignment="Top" Width="131" Style="{DynamicResource StackPanelStyle1}" Background="Black"/>
</Grid>
</Window>
Use Expression Blend 4 auto generated.
Some style is very trouble to use hand-code.