I am relatively new to WPF, but I would like to know how can I enable a listbox to select an item based on a mouseover event instead of the button click. I would like the item to be selected when the mouse is over the selected item, without having to press click first.
Thank you
You may write a simple ListBoxItem Style with a Trigger on the IsMouseOver property that sets the IsSelected property:
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Related
I have a TextBox inside my ListView. When I click on the textview, the SelectionChanged event of ListView is not fired.
Is there a way to fire that, so that the ListView item is selected?
Despite the fact that you have not asked a good question, I think I understand your problem. I'm assuming that when you said textview, you actually meant Textbox and that your problem is that your ListViewItem is not changed to the item that contains the TextBox that you clicked on. If this is so, then adding this Style should do the trick:
<Style x:Key="ListViewItemSelectionStyle" TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="False">
<Setter Property="IsSelected" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
While it looks strange, the second part is to ensure that the item remains selected after it loses KeyboardFocus.
You haven't provided any code, but at a guess, you haven't handled the event:
<ListView SelectionChanged="MyEventHandler" ...
</ListView>
I am able to get a TextBlock in XAML to have an IsMouseOver trigger, but what about an IsPressed or IsFocused. I want the TextBlock background to change color when the user clicks on the TextBlock. This is TextBlock NOT a TextBox. It there a way to do it in only XAML.
I tried:
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="Blue"></Setter>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Blue"></Setter>
</Trigger>
And these don't seem to work at all. Is there another property or is it even possible to do on a TextBlock.
Thanks in advance.
Wrap it in a ToggleButton, change ToggleButton.Template to a lone ContentPresenter in a Border. Hook up Background to Border.Background via TemplateBinding.
This gives you IsChecked to trigger on and just shows text with a background color.
I have a list view in which each list view item is an expander control. Now when I click on expander to open or close it, I want the item to be selected also. But its not happening. Only expander expands.
Looks like my event from expander is not reaching to the list view.
Any idea how to do it?
Thanks
Inside the "ListView" set the container style like this...
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
Got answer from:
Link
I have a UserControl which contains a ContentControl. When the user clicks this ContentControl I want to change its ContentTemplate, to make it "editable" (instead of labels display textboxes for example).
What I have is this:
<StackPanel>
<ContentControl Style="{DynamicResource ContainerStyleEditable}" GotFocus="ContentControl_GotFocus"></ContentControl>
</StackPanel>
and in userControl resources i have
<Style TargetType="{x:Type ContentControl}" x:Key="ContainerStyleEditable">
<Setter Property="ContentTemplate" Value="{DynamicResource ItemTemplateReadOnly}" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="ContentTemplate" Value="{DynamicResource ItemTemplateEditable}" />
</Trigger>
</Style.Triggers>
</Style>
This doe not work, it seems the GotFocus event never fires. What is the way to to this?
I usually base my triggers of IsKeyboardFocusWithin instead of IsFocused because often the focused element usually isn't the actual ContentControl, but rather a control inside it's Content.
Also, be sure that at least one control inside the ContentControl can accept focus so the control can get focus. If nothing inside the control can accept focus, your trigger will never fire.
I am using Expander in WPF to display my data. The default style for the Expander control contains a toggle button which shows/hides my content when I click on it.
How can I modify the style so that it expands when I hovers the mouse over the header and collapse when I move away?
Barebone setup should be this:
<Style TargetType="{x:Type Expander}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsExpanded" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
(Applies to the whole expander, not just the header. That would probably require messing with the template.)
It is possible to use databinding between isExpanded property an ismouseover:
IsExpanded="{Binding IsMouseOver, Mode=OneWay, RelativeSource={RelativeSource Self}}"