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
- Tomasz Cholewinski asked 8 years ago
- last edited 8 years ago
- You must login to post comments
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 …
- Andrew Burnett-Thompson answered 8 years ago
-
Works like a charm, thank you
- You must login to post comments
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);
}
}
- Tomasz Cholewinski answered 8 years ago
-
It’s a great solution, thanks for sharing!
-
What would the XAML look like that goes with this?
- You must login to post comments
Please login first to submit.