PlaTyPuS8

Hi!

is it possible to select the item when the mouse is over it

thx!



Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??


Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??

Josh Smith

If you don't want a brief pause between "mouse over" and "item select" then you could bind the ListViewItem's IsSelected to it's IsMouseOver property.

If you want there to be a momentary pause before selecting, the you should start a DispatcherTimer when the mouse enters the item, and select the item when the timer ticks.






Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??

PlaTyPuS8

thx for answers!

Josh, your solution works well, but how can I apply it to the style of a listviewitem

The binding I made for a listview with static items:

Code Snippet

<ListViewItem Content="ListViewItem" x:Name="ListViewItem1" IsSelected="{Binding Path=IsMouseOver, ElementName=ListViewItem1, Mode=OneWay}"/>

And I try to bind it in the style to the <Trigger Property="IsSelected" Value="true"> but it doesn't work (can't bind the Value of a trigger property...).





Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??

Frances83

You can create a Style with a Trigger for the IsMouseOver-event, and then set the ItemContainerStyle of your ListView to that Style.

Code Snippet

<Style x:Key="mouseOverEvent" TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>

<ListView Name="lstList" ItemContainerStyle="{StaticResource mouseOverEvent}" >






Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??

PlaTyPuS8

don't seems to work! it's only highlighting the item! Here's my code :

Code Snippet

...myStyleCode...

<ControlTemplate.Triggers>

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

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

<Setter Property="Background" Value="{StaticResource ListItemSelectedFill}"/>

<Setter Property="BorderBrush" Value="#FF98DDFB"/>

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

Yi-Lun Luo - MSFT posted the following code in the post proposed by lee :

Code Snippet

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

<DataTemplate>

<StackPanel MouseEnter="Select">

</< SPAN>StackPanel>

</< SPAN>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;

}

But I don't understand the DataTemplate and StackPanel.....

thx for help





Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??

PlaTyPuS8

little up



Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??

PlaTyPuS8

PlaTyPuS8 wrote:

thx for answers!

Josh, your solution works well, but how can I apply it to the style of a listviewitem

The binding I made for a listview with static items (this works!):

Code Snippet

<ListViewItem Content="ListViewItem" x:Name="ListViewItem1" IsSelected="{Binding Path=IsMouseOver, ElementName=ListViewItem1, Mode=OneWay}"/>

an idea about how to do that with dynamic items

why is this code just highlighting the row

<ControlTemplate.Triggers>

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

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

...




Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??

PlaTyPuS8

up



Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??

PlaTyPuS8

up



Re: Windows Presentation Foundation (WPF) Select ListViewItem OnMouseOver ??

PlaTyPuS8

re-up