Question

Sorry if this is a newbie question but, well, i am... and i've been trying to do this for 2 days now, searching the web and asking arround, and nobody could help me, so any insight will be appreciated...

What i basically need to do is select a chart series (ColumnSeries or LineSeries) datapoint with rightclick before i open a contextual menu. I need the value of the x axis of the datapoint when i handle the menuItemClick event. Ideally i'd like to be able to raise the SelectionChanged event on the rightclick as well as on the leftclick, but haven't found any way to do it. I've tried some alternatives but kept hitting dead ends. This solution seemed to do what i wanted for a LineSeries: Select the nearest point in a Silverlight Toolkit chart, but 90% of my series are ColumnSeries and don't have the serie.Points property so i'm stuck again...

Était-ce utile?

La solution

In the end i used this solution: http://www.c-sharpcorner.com/uploadfile/baimey/silverlight-charts-coordinates-on-mousemove/

I thought i'd better post it in case someone else needs this.

    private void Chart_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
    {
        Series serie = sender as Series;
        DateTime xAxisValue;
        Point p = e.GetPosition(serie);
        ICategoryAxis xAxis = (CategoryAxis)Chart.ActualAxes[0];
        object xHit = xAxis.GetCategoryAtPosition(new UnitValue(p.X, Unit.Pixels));

        if (SelectedObj != null)
        {
            xAxisValue = (DateTime)new DateTimeConverter().ConvertBack(xHit as String, typeof(String), SelectedObj.DisplayFrequency, Thread.CurrentThread.CurrentUICulture);

            foreach (Frequency frequency in SelectedObj.Frequencies)
            {
                if(frequency == SelectedObj.DisplayFrequency)
                    addMenuItem(frequency, true, xAxisValue);
                else
                    addMenuItem(frequency, false, xAxisValue);
            }

            cMenu.IsOpen = true;
            cMenu.HorizontalOffset = e.GetPosition(LayoutRoot).X;
            cMenu.VerticalOffset = e.GetPosition(LayoutRoot).Y;
        }
    }

    private void addMenuItem(Frequency frequency, bool isDisplayFrequency, DateTime xAxisValue)
    {
        menuItem = new MenuItem();
        menuItem.Header = frequency;
        menuItem.Tag = xAxisValue;
        if (isDisplayFrequency)
        {
            menuItem.Icon = new TextBlock { Text = "\xfc", FontFamily = new System.Windows.Media.FontFamily("Wingdings"), FontWeight = FontWeights.Bold, TextAlignment = TextAlignment.Center };
            menuItem.FontWeight = FontWeights.Bold;
        }
        cMenu.Items.Add(menuItem);
        menuItem.Click += new RoutedEventHandler(menuItem_Click);
    }

    void menuItem_Click(object sender, RoutedEventArgs e)
    {
        MenuItem item = sender as MenuItem;
        Frequency itemFreq = (Frequency)item.Header;
        DateTime xAxisValue = (DateTime)item.Tag;

        ...

        cMenu.IsOpen = false;
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top