1
我可能忽視的東西時,我看到的解決方案,將是顯而易見的,但現在...的Silverlight 3.0自定義光標在圖表
我試圖使用自定義光標圖表區一個工具包內圖表。我爲圖表創建了一個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