图表中的Silverlight 3.0自定义光标

时间:2010-04-14 13:59:31

标签: silverlight silverlight-3.0 charts silverlight-toolkit custom-cursor

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

我试图在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

1 个答案:

答案 0 :(得分:0)

我有一件事可行(用Canvas围绕它),但我会接受其他代码味道较少的替代品。

<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>