Created
August 9, 2019 22:07
-
-
Save LanceMcCarthy/def58bf363df771b34cc7d9877ee7c45 to your computer and use it in GitHub Desktop.
Custom Commandable ChartSelectionBehavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<telerikChart:RadCartesianChart> | |
<telerikChart:RadCartesianChart.ChartBehaviors> | |
<local:MyChartSelectionBehavior Command="{Binding MyCommand}" DataPointSelectionMode="Single" SeriesSelectionMode="None"/> | |
</telerikChart:RadCartesianChart.ChartBehaviors> | |
<!-- See the following documentation for the rest of the code https://docs.telerik.com/devtools/xamarin/controls/chart/behaviors/chart-behaviors-selection-behavior --> | |
</telerikChart:RadCartesianChart> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyChartSelectionBehavior : ChartSelectionBehavior | |
{ | |
public MyChartSelectionBehavior() | |
{ | |
this.SelectionChanged += MyChartSelectionBehavior_SelectionChanged; | |
} | |
public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(MyChartSelectionBehavior), null); | |
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(MyChartSelectionBehavior), null); | |
public static readonly BindableProperty InputConverterProperty = BindableProperty.Create("Converter", typeof(IValueConverter), typeof(MyChartSelectionBehavior), null); | |
public ICommand Command | |
{ | |
get => (ICommand)GetValue(CommandProperty); | |
set => SetValue(CommandProperty, value); | |
} | |
public object CommandParameter | |
{ | |
get => GetValue(CommandParameterProperty); | |
set => SetValue(CommandParameterProperty, value); | |
} | |
public IValueConverter Converter | |
{ | |
get => (IValueConverter)GetValue(InputConverterProperty); | |
set => SetValue(InputConverterProperty, value); | |
} | |
// When the event fires, invoke the command | |
private void MyChartSelectionBehavior_SelectionChanged(object sender, EventArgs e) | |
{ | |
if (Command == null) | |
{ | |
return; | |
} | |
object resolvedParameter; | |
if (CommandParameter != null) | |
{ | |
resolvedParameter = CommandParameter; | |
} | |
else if (Converter != null) | |
{ | |
resolvedParameter = Converter.Convert(e, typeof(object), null, null); | |
} | |
else | |
{ | |
resolvedParameter = e; | |
} | |
if (Command.CanExecute(resolvedParameter)) | |
{ | |
Command.Execute(resolvedParameter); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment