Scott Croisdale

Hi everyone,

Im trying to create a list box that selects items when you "mouse over" them instead of having to click them... i dont suppose theres a simply way to do this is there.. im going out of my mind!

Kind Regards,

Scott.



Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

lee d

can you try

<Style TargetType="{x:Type ListBoxItem}">

<Style.Triggers>

<Trigger Property="IsMouseOver" Value="True">

<Setter Property="IsSelected" Value="True"></Setter>

</Trigger>

</Style.Triggers>

</Style>






Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

Scott Croisdale

That kind of works, the item doesnt stay selected though when i mouse off (kind of obviously i guess!).

The other odd thing is that it doesnt automatically highlight the first item in the list, but all the others are fine.... any ideas

Cheers!

S>





Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

lee d

just realized, the properties wont be sticking, like you said




Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

lee d

this should work better

add this declaration

Code Snippet

xmlns:sys="clr-namespace:System;assembly=mscorlib"

and change the style to

<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected" >
<DiscreteObjectKeyFrame KeyTime="0:0:0" >
<DiscreteObjectKeyFrame.Value>
<sys:Boolean>True</sys:Boolean>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>






Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

Scott Croisdale

That solution seems to just highlight every list item as i mouse over, without deselecting the previously selected one - despite the fact i have the listbox set to "single item" selection mode....

It also still doesnt select the first node - which i believe is already "selected" under the covers when you set data on the list.





Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

lee d

I am not sure on how to unselect the previous Item, I dont think listbox will select the first item by default




Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

Scott Croisdale

I dont suppose anyone else has any ideas do they Im finding this really hard!

Im basically trying to create an office 2007 style "start menu" component, with a master/detail binding list in it. I have made the component with two lsitboxes but need them to react to mouse over rather than simply mouseclick.

Thanks for your help.





Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

Douglas Stockwell

Looks like you will have to write some code, one possible solution:

<my:HoverListBox xmlns:my="clr-namespace:...">

public class HoverListBox : System.Windows.Controls.ListBox
{
  protected override DependencyObject GetContainerForItemOverride()
  {
    return new HoverListBoxItem();
  }
}

public class HoverListBoxItem : System.Windows.Controls.ListBoxItem
{
  protected override void OnMouseEnter(MouseEventArgs e)
  {
    this.IsSelected = true;
  }
}





Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

lee d

yes, I think writing some code is better in this case




Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

Yi-Lun Luo - MSFT

You can achieve this by using DataTemplate. Suppose you have a DataTemplate:

<DataTemplate>

<StackPanel MouseEnter="Select">

</StackPanel>

</DataTemplate>

In code behind, you can write:

private void Select(object sender, MouseEventArgs e)

{

StackPanel sp = (StackPanel)sender;

ContentPresenter cp = (ContentPresenter)sp.TemplatedParent;

ListBoxItem item = (ListBoxItem)cp.TemplatedParent;

item.IsSelected = true;

}






Re: Windows Presentation Foundation (WPF) ListBox with item selection on "highlight"

Scott Croisdale

Well it works, but theres a big lag between mouse over and the actual highlight appearing... as far as i can work out this is just down to the listbox selection routine Its too slow to use really.