Skip to content

Instantly share code, notes, and snippets.

@LanceMcCarthy
Created August 9, 2019 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LanceMcCarthy/def58bf363df771b34cc7d9877ee7c45 to your computer and use it in GitHub Desktop.
Save LanceMcCarthy/def58bf363df771b34cc7d9877ee7c45 to your computer and use it in GitHub Desktop.
Custom Commandable ChartSelectionBehavior
<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>
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