将行放在ItemsContol上,在WPF中覆盖IsMouseOver属性

时间:2015-03-30 13:32:45

标签: c# wpf

我有Canvas,显示来自ObservableCollection<T>的行,以及在Canvas上点击左右鼠标后的2行,以及临时Line },总是在鼠标指针下。这是代码:

<Canvas Height="100" Margin="5" Background="Transparent" Name="MeasureCanvas"
                MouseDown="Canvas_MouseDown" MouseEnter="Canvas_MouseEnter"
                MouseMove="Canvas_MouseMove" MouseLeave="Canvas_MouseLeave">

            <ItemsControl Name="Lines" ItemsSource="{Binding SnapLines}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Canvas>
                            <Thumb DragDelta="onDragDelta" Canvas.Left="0" Canvas.Top="0">
                                <Thumb.Template>
                                    <ControlTemplate>
                                        <Line Style="{StaticResource LeftLineStyle}" MouseEnter="Line_MouseEnter" MouseLeave="Line_MouseLeave" Name="LeftLine" />
                                    </ControlTemplate>
                                </Thumb.Template>
                            </Thumb>
                            <Thumb DragDelta="onDragDelta" Canvas.Left="0" Canvas.Top="0">
                                <Thumb.Template>
                                    <ControlTemplate>
                                        <Line Style="{StaticResource RightLineStyle}" MouseEnter="Line_MouseEnter" MouseLeave="Line_MouseLeave" Name="RightLine" />
                                    </ControlTemplate>
                                </Thumb.Template>
                            </Thumb>
                            <arrow:ArrowLine Style="{StaticResource ConnectingLineStyle}" />
                        </Canvas>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
            <Line Style="{StaticResource MeasureLine1Style}"   />
            <Line Style="{StaticResource MeasureLine2Style}" />
            <arrow:ArrowLine Style="{StaticResource MeasureArrowStyle}"   />

            <Line Style="{StaticResource MeasureTempLineStyle}" />
        </Canvas>

线条:

<Style x:Key="MeasureLine1Style" TargetType="Line">
    <Setter Property="X1" Value="{Binding MeasureLine1.X}" />
    <Setter Property="X2" Value="{Binding MeasureLine1.X}" />
    <Setter Property="Y1" Value="0" />
    <Setter Property="Y2" Value="200" />
    <Setter Property="Stroke" Value="White" />
    <Setter Property="StrokeThickness" Value="2" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding MeasureLine1.X}" Value="0">
            <Setter Property="Visibility" Value="Hidden"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>
<Style x:Key="MeasureLine2Style" TargetType="Line">
    <Setter Property="X1" Value="{Binding MeasureLine2.X}" />
    <Setter Property="X2" Value="{Binding MeasureLine2.X}" />
    <Setter Property="Y1" Value="0" />
    <Setter Property="Y2" Value="200" />
    <Setter Property="Stroke" Value="White" />
    <Setter Property="StrokeThickness" Value="2" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding MeasureLine2.X}" Value="0">
            <Setter Property="Visibility" Value="Hidden"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>
<Style x:Key="MeasureTempLineStyle" TargetType="Line">
    <Setter Property="X1" Value="{Binding MeasureTemp.X}" />
    <Setter Property="X2" Value="{Binding MeasureTemp.X}" />
    <Setter Property="Y1" Value="0" />
    <Setter Property="Y2" Value="200" />
    <Setter Property="Stroke" Value="{StaticResource SelectedLineGreen}" />
    <Setter Property="StrokeThickness" Value="2" />

    <Setter Property="Visibility" Value="Hidden" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding MeasuringPlacingLine}" Value="True">
            <Setter Property="Visibility" Value="Visible"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

以及Lines中显示的ItemsControl

    <Setter Property="X1" Value="{Binding Point1.X}" />
    <Setter Property="X2" Value="{Binding Point1.X}" />
    <Setter Property="Y1" Value="0" />
    <Setter Property="Y2" Value="200" />
    <Setter Property="Stroke" Value="White" />
    <Setter Property="StrokeThickness" Value="3" />
    <Setter Property="Margin" Value="{Binding PointMargin}" />

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect ShadowDepth="0" Color="White" Opacity="1" BlurRadius="20" />
                </Setter.Value>
            </Setter>
            <Setter Property="Stroke" Value="{StaticResource LineGreen}" />
        </Trigger>
        <DataTrigger Binding="{Binding Point1.X}" Value="0">
            <Setter Property="Visibility" Value="Hidden"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>
<Style x:Key="RightLineStyle" TargetType="Line">
    <Setter Property="X1" Value="{Binding Point2.X}" />
    <Setter Property="X2" Value="{Binding Point2.X}" />
    <Setter Property="Y1" Value="0" />
    <Setter Property="Y2" Value="200" />

    <Setter Property="Margin" Value="{Binding PointMargin}" />
    <Setter Property="Stroke" Value="White" />
    <Setter Property="StrokeThickness" Value="3" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding Point2.X}" Value="0">
            <Setter Property="Visibility" Value="Hidden"></Setter>
        </DataTrigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect ShadowDepth="0" Color="White" Opacity="1" BlurRadius="20" />
                </Setter.Value>
            </Setter>
            <Setter Property="Stroke" Value="{StaticResource LineGreen}" />
        </Trigger>
    </Style.Triggers>
</Style>

因此,当用户将鼠标悬停在Line的已放置ItemsControl上时,它会发光,并且用户会拖动DragDelta

我的问题是,这只有在我注释掉以下行时才有效:

<Line Style="{StaticResource MeasureTempLineStyle}" />

当显示临时线时,临时线获得IsMouseOver属性,因此来自Line的已放置ItemsControl不会收到此属性。见图片:

enter image description here

1 个答案:

答案 0 :(得分:0)

在临时线上将IsHitTestVisible设为false。这有效地使鼠标不可见。