SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy. We also have a tag=SciChart on Stackoverflow.com where you can earn rep for your questions!

Please note: SciChart team will only answer questions from customers with active support subscriptions. Expired support questions will be ignored. If your support status shows incorrectly, contact us and we will be glad to help.

Answered
1
1

Hello,

My goal is to have the user select a series with a mouse click, and to see which series was selected while following the MVVM pattern. I am filling in the DataSeries using a LineSeriesSource attached behaviour.

What I tried so far is to attach to the SelectionChanged event of a Renderable series with an Interaction Trigger.

This doesn’t work:

        <SciChart:SciChartSurface.ChartModifier>

            <SciChart:ModifierGroup>

                <SciChart:SeriesSelectionModifier >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged" >
                            <i:InvokeCommandAction Command="{Binding Path=cmdSelectedSeriesChanged}" CommandParameter="{Binding Path=SelectedSeries}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <SciChart:SeriesSelectionModifier.SelectedSeriesStyle>
                        <Style TargetType="SciChart:BaseRenderableSeries">
                            <Setter Property="Stroke" Value="White"/>
                            <Setter Property="StrokeThickness" Value="2"/>
                        </Style>
                    </SciChart:SeriesSelectionModifier.SelectedSeriesStyle>
                </SciChart:SeriesSelectionModifier>

        </SciChart:ModifierGroup>
        </SciChart:SciChartSurface.ChartModifier>

This works, but is not what I want since it’s not MVVM:

<SciChart:SciChartSurface.ChartModifier>

                <SciChart:ModifierGroup>

                    <SciChart:SeriesSelectionModifier SelectionChanged="SeriesSelectionModifier_SelectionChanged">

                        <SciChart:SeriesSelectionModifier.SelectedSeriesStyle>
                            <Style TargetType="SciChart:BaseRenderableSeries">
                                <Setter Property="Stroke" Value="White"/>
                                <Setter Property="StrokeThickness" Value="2"/>
                            </Style>
                        </SciChart:SeriesSelectionModifier.SelectedSeriesStyle>
                    </SciChart:SeriesSelectionModifier>

            </SciChart:ModifierGroup>
            </SciChart:SciChartSurface.ChartModifier>

Interaction Propety cannot be attached to SciChartSurface.RenderableSeries.
I also tried attaching the Interaction Property to an individual DataSeries, which is definitely not what I want to do, but it doesn’t work either.

/Tomasz

  • You must to post comments
Best Answer
1
0

Someone reported this before, and the SelectionChanged event does fire, but the i:EventTrigger is not picking it up. Probably because it’s not a RoutedEvent, but a normal, .NET Event.

What we suggested to them to was to make their own attached property which listened to the event, and passed the call to the ViewModel.

e.g.

public class SelectionModifierExtensions
{
    public static readonly DependencyProperty SelectionChangedCommandProperty = DependencyProperty.RegisterAttached(
        "SelectionChangedCommand", typeof(ICommand), typeof(SelectionModifierExtensions), new PropertyMetadata(default(ICommand), OnValueChanged));

    private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var newModifier = d as SeriesSelectionModifier;
        if (newModifier != null)
        {
            newModifier.SelectionChanged += (s, arg) =>
            {
                var command = e.NewValue as ICommand;
                if (command != null)
                {
                    command.Execute(null);
                }
            };
        }
    }

    public static void SetSelectionChangedCommand(DependencyObject element, ICommand value)
    {
        element.SetValue(SelectionChangedCommandProperty, value);
    }

    public static ICommand GetSelectionChangedCommand(DependencyObject element)
    {
        return (ICommand)element.GetValue(SelectionChangedCommandProperty);
    }
}

Usage

 <s:SeriesSelectionModifier SelectionModifierExtensions.SelectionChangedCommand="{Binding SelectionChangedCommand}"/>

The exact same principle applies to the DataPointSelectionModifier if anyone comes across that problem …

  • You must to post comments
1
0

I ended up extending the SeriesSelectionModifier because the SelectionChanged Event doesn’t carry information about which series is selected at the time. Not sure if this is the preferred way, but it works

class ExtendedSeriesSelectionModifier : SeriesSelectionModifier
{
    public IRenderableSeries SelectedSeries
    {
        get { return GetValue(SelectedSeriesProperty) as IRenderableSeries; }
        set { SetValue(SelectedSeriesProperty, value); }
    }
    public static DependencyProperty SelectedSeriesProperty = 
        DependencyProperty.Register("SelectedSeries", typeof(IRenderableSeries), 
            typeof(ExtendedSeriesSelectionModifier),
          new FrameworkPropertyMetadata(default(IRenderableSeries), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    protected override void OnSelectedSeriesChanged(IEnumerable<IRenderableSeries> oldSeries, IEnumerable<IRenderableSeries> newSeries)
    {
        if (newSeries != null)
        {
            SelectedSeries = newSeries.First();
        }
        else
        {
            SetValue(SelectedSeriesProperty, null);
        }
        base.OnSelectedSeriesChanged(oldSeries, newSeries);
    }
}
  • Andrew
    It’s a great solution, thanks for sharing!
  • Christopher Bennet
    What would the XAML look like that goes with this?
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.