就是在鼠标滑过的时候该项有颜色变化
网上找到个C#的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,就卡住了
希望大家帮忙~~谢谢

解决方案 »

  1.   

    是鼠标滑过listBoxItem发生变化吗?可以用触发器IsMouseOver,改变背景色或者前景色
      

  2.   

    我觉得可以做个模板,让listBoxItem都应用这个模板风格
      

  3.   

    <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>
    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("碰到我就变色。");
            }
    }
    }
      

  4.   

    Style for ListBox
    单独写一个Style给ListBoxItem,即ItemContainerStyle
    增加Trigger对应MouseHover
      

  5.   

    强烈感谢 cmis7645
    结贴给分~