Pre loader

MVVM Binding RenderableSeries SelectionChanged event to handler with Interaction Trigger

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

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);
    }
}
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies