Style code:
<Style x:Key="SpacesQuestionEditor" TargetType="Editor">
<Setter Property="behaviors:TestEditorStyleBehavior.AttachBehavior">
<Setter.Value>
<toolkit:EventToCommandBehavior EventName="TextChanged"
Command="{Binding Source={x:Reference TestEditorPage}, Path=BindingContext.EditorTextChangedCommand}"
CommandParameter="{Binding .}"/>
</Setter.Value>
</Setter>
</Style>
Editor code:
<Editor x:Name="spacesTextEditor" Placeholder="Enter text" Style="{StaticResource SpacesQuestionEditor}" Text="{Binding SpacesEditorText}" AutoSize="TextChanges"/>
Command from view model code:
[RelayCommand]
public void EditorTextChanged(TestQuestion testQuestion)
{
ChangeHtml(testQuestion);
}
Attach behavior code:
public class TestEditorStyleBehavior : Behavior<Editor>
{
public static readonly BindableProperty AttachBehaviorProperty = BindableProperty.CreateAttached(
propertyName: "AttachBehavior",
returnType:typeof(object),
declaringType: typeof(TestEditorStyleBehavior),
defaultValue: null,
propertyChanged: OnAttachBehaviorChanged);
public static object GetAttachBehavior(BindableObject view)
{
return (object)view.GetValue(AttachBehaviorProperty);
}
public static void SetAttachBehavior(BindableObject view, object value)
{
view.SetValue(AttachBehaviorProperty, value);
}
static void OnAttachBehaviorChanged(BindableObject view, object oldValue, object newValue)
{
Editor editor = view as Editor;
if (editor == null)
{
return;
}
EventToCommandBehavior attachBehavior = newValue as EventToCommandBehavior;
editor.Behaviors.Add(attachBehavior);
}
}
I am attaching event to my command but i dont understand how to get sender and event args inside my command. I need both of this arguments for my needs. I understand how to pass whole editor object or just model from data type but dont understand how to get event paramethers. Can u please explain how to do this?
You can try to create a custom view for your Editor.
I achieved this function by inheriting parent class
Entry
. The same applies to Editor.You can refer to the following code:
1.create a class
MyEntry.cs
and add necessaryBindableProperty
.2.created a viewmodel (MyViewModel.cs)
3.Usage example: