添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
爱看球的蟠桃  ·  linux 海康 sdk ...·  2 月前    · 
善良的番茄  ·  qcustomplot ...·  3 月前    · 

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account
<ListBox ItemsSource="{Binding SignalAnalysisResultItems}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="sdassad"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

Above code is my sample xaml.
If I execute my application, then show the below result.

Color of first row is lightblue when MouseHover on ListBox.
Color of second row is deepblue when RightMouseClick on ListBox.

I want to change background when MouseHover And RightMouseClick Event. But I don't know how to change this it .

changed the title How to change backgroudn of selected item by left mouse click and right mouse click? How to change background of selected item by left mouse click and right mouse click? Aug 6, 2020 changed the title How to change background of selected item by left mouse click and right mouse click? How to change background of selected item and MouseHover item? Aug 6, 2020

@Hyung-Kim For the hover background you can override the ListBoxItem style like this:

<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource MahApps.Styles.ListBoxItem}">
    <Setter Property="Controls:ItemHelper.HoverBackgroundBrush" Value="{DynamicResource MahApps.Brushes.Gray}" />
    <Setter Property="Controls:ItemHelper.HoverSelectedBackgroundBrush" Value="{DynamicResource MahApps.Brushes.Gray1}" />
</Style>

For the RightMouseClick event it should be possible to do this, but it doesn't work with the current style. For this I must fix the ListBoxItem style.

<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource MahApps.Styles.ListBoxItem}">
    <Setter Property="Controls:ItemHelper.HoverBackgroundBrush" Value="{DynamicResource MahApps.Brushes.Gray}" />
    <Setter Property="Controls:ItemHelper.HoverSelectedBackgroundBrush" Value="{DynamicResource MahApps.Brushes.Gray1}" />
    <Style.Triggers>
        <EventTrigger RoutedEvent="PreviewMouseRightButtonDown">
            <BeginStoryboard x:Name="BeginStoryboard_IsPressed">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Duration="0:0:0"
                                                   Storyboard.TargetProperty="Background">
                            Note: It's not possible to use DynamicResource inside a Storyboard.
                            So use StaticResource or a static Brush instead.
                        <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Brushes.Red}" />
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="PreviewMouseRightButtonUp">
            <RemoveStoryboard BeginStoryboardName="BeginStoryboard_IsPressed" />
        </EventTrigger>
    </Style.Triggers>
</Style>

I will close this issue and create a new one for the ListBoxItem style changes.