简体   繁体   English

用于MouseOver的DataTemplate ListBoxItem

[英]DataTemplate ListBoxItem for MouseOver

I am trying to get a better understanding of the Templating system with WPF. 我试图通过WPF更好地理解模板系统。 My design goal here is that I am trying to re-design a component of a software package I work on to use WPF instead of 2D Graphics Librarys. 我的设计目标是,我正在尝试重新设计我使用的软件包的组件,以使用WPF而不是2D Graphics Librarys。 The problem right now is that with 2000 entitys on the screen, pan and zoom is very laggy on a beefy i7 workstation. 现在的问题是屏幕上有2000个实体,在强大的i7工作站上平移和缩放是非常滞后的。 I wish to redevelop it in WPF to improve performance. 我希望在WPF中重新开发它以提高性能。

Just as a test, I have created a new project and created a basic XAML/WPF View with a Canvas. 就像测试一样,我创建了一个新项目并使用Canvas创建了一个基本的XAML / WPF视图。 This canvas displays ellipses (essentially I am building a 2D Map for planning purposes which has circles of different colors to show different things). 此画布显示省略号(基本上我正在构建用于规划目的的2D地图,其中包含不同颜色的圆圈以显示不同的内容)。

Now, I have my circles showing up fine using a DataTemplate depending on what sort of model the circle is (Circle A or Circle B). 现在,根据圆圈的模型(圆圈A或圆圈B),我可以使用DataTemplate显示我的圆圈。 When I mouse over the circle, I have the windows default 'blue box' area behind it, which I wish to remove. 当我将鼠标放在圆圈上时,我的窗口后面有默认的“蓝框”区域,我希望将其移除。 So far I have managed to use a trigger to change the circle color on mouse over, but I still have the hover area. 到目前为止,我已经设法使用触发器来改变鼠标上的圆形颜色,但我仍然有悬停区域。

在此输入图像描述

<DataTemplate x:Key="HoleBTemplate">
    <Grid Width="40" Height="40">
        <Ellipse>
            <Ellipse.Style>
                <Style TargetType="Ellipse">
                    <Setter Property="Fill" Value="DeepSkyBlue"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Fill" Value="Yellow" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Ellipse.Style>
        </Ellipse>
        <Ellipse Fill="White" Margin="1" IsHitTestVisible="False" />
        <TextBlock 
            VerticalAlignment="Center"
            TextAlignment="Center"
            HorizontalAlignment="Center"
            Text="{Binding Name}"
            IsHitTestVisible="False">
        </TextBlock>
        <Grid.RenderTransform>
            <TranslateTransform X="-20" Y="-20" />
        </Grid.RenderTransform>
    </Grid>
</DataTemplate>

I created my own ListBox Control which positions the ListBoxItems at an X,Y location inside a canvas. 我创建了自己的ListBox控件,它将ListBoxItems定位在画布内的X,Y位置。

<controls:ListBoxMap ItemsSource="{Binding Holes}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas></Canvas>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplateSelector>
        <controls:PlanTemplateSelector 
            HoleATemplate="{StaticResource HoleATemplate}"
            HoleBTemplate="{StaticResource HoleBTemplate}"/>
    </ItemsControl.ItemTemplateSelector>
</controls:ListBoxMap>

TLDR: How do I get rid of the blue highlight area behind the circles on mouse over? TLDR:如何摆脱鼠标悬停在圆圈后面的蓝色高光区域?

Answer from linked thread above, by Wayne Lo. Wayne Lo从上面的链接线程回答。

<Style TargetType="ListBoxItem">
    <Setter Property="IsSelected" Value="{Binding Content.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <ContentPresenter/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM