1

当我看到解决方案时,我可能忽略了一些显而易见的事情,但现在......

我正在尝试在 Toolkit 图表的图表区域内使用自定义光标。我为图表创建了一个 ControlTemplate,并为包含光标创建了一个网格。我显示/隐藏光标,并尝试使用各种鼠标事件移动包含的网格。光标显示在正确的时间,但我无法让它移动到正确的位置。

这是 ControlTemplate(时髦的颜色只是试图确认模板的不同部分属于什么):

        <dataVisTK:Title Content="{TemplateBinding Title}"
                         Style="{TemplateBinding TitleStyle}"/>

        <Grid Grid.Row="1">

            <!-- Remove the Legend -->
            <!--<dataVisTK:Legend
        x:Name="Legend"
        Title="{TemplateBinding LegendTitle}"
        Style="{TemplateBinding LegendStyle}"
        Grid.Column="1"/>-->


            <chartingPrimitivesTK:EdgePanel x:Name="ChartArea" 
                                            Background="#EDAEAE"
                                            Style="{TemplateBinding ChartAreaStyle}"
                                            Grid.Column="0">
                <Grid Canvas.ZIndex="-1" 
                      Background="#2008AE"
                      Style="{TemplateBinding PlotAreaStyle}">
                </Grid>
                <Border Canvas.ZIndex="1" 
                        BorderBrush="#FF250010" 
                        BorderThickness="3" />

                <Grid x:Name="gridHandCursors" 
                      Canvas.ZIndex="5"
                      Width="32" Height="32"
                      Visibility="Collapsed">
                    <Image x:Name="cursorGrab" Width="32"
                           Source="Resources/grab.png" />
                    <Image x:Name="cursorGrabbing" Width="32"
                           Source="Resources/grabbing.png"
                           Visibility="Collapsed"/>
                </Grid>

            </chartingPrimitivesTK:EdgePanel>
        </Grid>                            
    </Grid>
</Border>

以下是鼠标事件(特别是 MouseMove):

void TimelineChart_Loaded(object sender, RoutedEventArgs e)
{
    chartTimeline.UpdateLayout();

    List<FrameworkElement> chartChildren = GetLogicalChildrenBreadthFirst(chartTimeline).ToList();

    mChartArea =
        chartChildren.Where(element => element.Name.Equals("ChartArea")).FirstOrDefault() as Panel;

    if (mChartArea != null)
    {
        grabCursor = chartChildren.Where(element => element.Name.Equals("cursorGrab")).FirstOrDefault() as Image;
        grabbingCursor = chartChildren.Where(element => element.Name.Equals("cursorGrabbing")).FirstOrDefault() as Image;

        mGridHandCursors =
            chartChildren.Where(element => element.Name.Equals("gridHandCursors")).FirstOrDefault() as Grid;

        mChartArea.Cursor = Cursors.None;
        mChartArea.MouseMove += new MouseEventHandler(mChartArea_MouseMove);
        mChartArea.MouseLeftButtonDown += new MouseButtonEventHandler(mChartArea_MouseLeftButtonDown);
        mChartArea.MouseLeftButtonUp += new MouseButtonEventHandler(mChartArea_MouseLeftButtonUp);
        if (mGridHandCursors != null)
        {
            mChartArea.MouseEnter += (s, e2) => 
                mGridHandCursors.Visibility = Visibility.Visible;
            mChartArea.MouseLeave += (s, e2) => 
                mGridHandCursors.Visibility = Visibility.Collapsed;
        }
    }
}

void mChartArea_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{            
    if (grabCursor != null)
        grabCursor.Visibility = Visibility.Visible;
    if (grabbingCursor != null)
        grabbingCursor.Visibility = Visibility.Collapsed;
}

void mChartArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (grabCursor != null)
        grabCursor.Visibility = Visibility.Collapsed;
    if (grabbingCursor != null)
        grabbingCursor.Visibility = Visibility.Visible;
}

void mChartArea_MouseMove(object sender, MouseEventArgs e)
{
    if (mGridHandCursors != null)
    {
        Point pt = e.GetPosition(null);
        mGridHandCursors.SetValue(Canvas.LeftProperty, pt.X);
        mGridHandCursors.SetValue(Canvas.TopProperty, pt.Y);
    }
}

任何通过此障碍的帮助将不胜感激!

谢谢,wTS

4

1 回答 1

0

我有一件可以工作的东西(用画布包围它),但我愿意接受其他更少代码气味的替代品。

<Canvas Canvas.ZIndex="5">
    <Grid x:Name="gridHandCursors" 
          Width="32" Height="32"
          Visibility="Collapsed">
        <Image x:Name="cursorGrab" Width="32"
               Source="Resources/grab.png" />
        <Image x:Name="cursorGrabbing" Width="32"
               Source="Resources/grabbing.png"
               Visibility="Collapsed"/>
    </Grid>
</Canvas>
于 2010-04-14T17:46:11.730 回答