Silverlight LineDataPoint增加MouseOver的大小

时间:2011-07-25 23:12:33

标签: silverlight xaml silverlight-toolkit

我在资源字典中为工具包提供的LineDataPoint使用自定义样式。

我希望数据点在鼠标悬停事件中增加,并在鼠标离开后恢复到原始大小。实现这个的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

我没有找到更好的解决方案,然后使用具有必要功能的扩展类:

public class ExtendedLineSeries : LineSeries
{
    //I assume that all points have the same size
    private double _originalDataPointWidth;
    private double _originalDataPointHeight;

    protected override DataPoint CreateDataPoint()
    {
        var dp = base.CreateDataPoint();

        if (this.IncreaseDataPointSizeTo != null)
        {
            dp.MouseEnter += new MouseEventHandler(OnDataPointMouseEnter);
            dp.MouseLeave += new MouseEventHandler(OnDataPointMouseLeave);
        }

        return dp;
    }
    /// <summary>
    /// The width and height to which the point is increased in size
    /// </summary>
    public double? IncreaseDataPointSizeTo { get; set; }

    void OnDataPointMouseLeave(object sender, MouseEventArgs e)
    {
        var dp = sender as DataPoint;
        if (dp != null)
        {
            //return to the original size
            dp.Width = _originalDataPointWidth;
            dp.Height = _originalDataPointHeight;
            dp.UpdateLayout();
            base.UpdateDataPoint(dp);
        }
    }

    void OnDataPointMouseEnter(object sender, MouseEventArgs e)
    {
        var dp = sender as DataPoint;
        if (dp != null)
        {
            //remember the original size and enlarge the data point
            _originalDataPointWidth = dp.ActualWidth;
            _originalDataPointHeight = dp.ActualHeight;
            dp.Width = dp.Height = IncreaseDataPointSizeTo.Value;
            dp.UpdateLayout();
            base.UpdateDataPoint(dp);
        }
    }
}

此类可以在使用公共LineSeries类的任何地方使用。它具有附加属性IncreaseDataPointSizeTo,其中包含悬停数据点的宽度和高度的最终大小。

xaml代码示例:

<charting:Chart>
    <charting:Chart.Series>
        <ext:ExtendedLineSeries IsSelectionEnabled="True"
                                IncreaseDataPointSizeTo="16" ...