首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WPF中想让LISTBOX有鼠标滑过效果,该如何解决

2012-05-13 
WPF中想让LISTBOX有鼠标滑过效果就是在鼠标滑过的时候该项有颜色变化网上找到个C#的C# codeprivatevoidlis

WPF中想让LISTBOX有鼠标滑过效果
就是在鼠标滑过的时候该项有颜色变化
网上找到个C#的

C# code
private   void   listBox1_MouseMove(object   sender,   MouseEventArgs   e) {         int   AIndex   =   ((ListBox)sender).IndexFromPoint(e.Location);         if   (AIndex   <   0)   return;         Text   =   ((ListBox)sender).Items[AIndex].ToString(); } 


但是WPF没有IndexFromPoint这个函数啊,也没有e.Location,就卡住了
希望大家帮忙~~谢谢

[解决办法]
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="listbox.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480" Loaded="Window_Loaded">
<Window.Resources>
<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="Bd">
<Setter.Value>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#8200958B" Offset="1"/>
<GradientStop Color="#7500FFED" Offset="0"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>


</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>

<Grid x:Name="LayoutRoot">
<ListBox HorizontalAlignment="Left" Margin="128,8,0,24" Width="172" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}" Name="listbox1" FontSize="16" />
</Grid>
</Window>


C# code
namespace listbox{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            this.InitializeComponent();            // 在此点下面插入创建对象所需的代码。        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            listbox1.Items.Add("碰到我就变色");            listbox1.Items.Add("碰到我就变色?");            listbox1.Items.Add("碰到我就变色!");            listbox1.Items.Add("碰到我就变色,");            listbox1.Items.Add("碰到我就变色。");        }    }}
[解决办法]
Style for ListBox
单独写一个Style给ListBoxItem,即ItemContainerStyle
增加Trigger对应MouseHover

热点排行