What I'm trying to accomplish
In a CollectionView
, I display a list of objects. In each display there's a checkbox next to it.
Ideally, when the user checks or un-checks the checkbox I go into a method where I can do stuff with the object:
The above is fantasy code.
The actual code
In reality, when the user checks/unchecks the box the code goes into a method, only I'm passing this object CheckedChangedEventArgs
into the method.
I need to convert that object ChcekedChangedEventArgs
into a bool. (Note that I'm using the MvvM
pattern, so I'm not working with code-behind.)
A kind soul gave me some code that will probably do the converting:
public class CheckedChangedArgsConverter : BaseConverterOneWay<CheckedChangedEventArgs, bool>
{
public override bool DefaultConvertReturnValue { get; set; } = false;
public override bool ConvertFrom(CheckedChangedEventArgs value, CultureInfo culture)
{
return value.Value;
}
}
However, I don't know how to use the converter. It seems like I have two options here:
- Run the converter from the Xaml and have it run before the command
AddRemoveFromList
is called so I can pass in the converted Boolean, or - Run the converter code from the C# class, something like this:
The fact is, I have no idea how to do either. Can anyone help?
====================================================================
EDIT: the object CheckedChangedEventArgs
getting passed into the C# method is ALWAYS null.
Here's my Checkbox
code. I'm using the EventToCommandBehavior
because it's the only thing I can think of to call a command from inside the Checkbox:
<CheckBox x:Name="checkBox"
ScaleX="1.5"
ScaleY="1.5"
Color="#000063"
HorizontalOptions="End">
<CheckBox.Behaviors>
<toolkit:EventToCommandBehavior EventName="CheckedChanged"
Command="{Binding Source={x:Reference this},
Path=BindingContext.AddRemoveFromListCommand}"/>
</CheckBox.Behaviors>
</CheckBox>
I added x:TypeArguments
to the Checkbox:
<CheckBox x:Name="checkBox"
ScaleX="1.5"
ScaleY="1.5"
Color="#000063"
HorizontalOptions="End">
<CheckBox.Behaviors>
<toolkit:EventToCommandBehavior
x:TypeArguments="CheckedChangedEventArgs"
EventName="CheckedChanged"
Command="{Binding Source={x:Reference this}, Path=BindingContext.AddRemoveFromListCommand}" />
</CheckBox.Behaviors>
</CheckBox>
And that did the trick.
You can just add a bool variable(e.g.
public bool VideoIsChecked
) to your Video model and implementINotifyPropertyChanged
for your Video Item(Suppose the Videl Model isVideo.cs
), then bind it to the propertyVideoIsChecked
of thisCheckBox
. Of course, if you use nuget MVVM Toolkit,you can simply this code.Once we check or uncheck the
CheckBox
, the value of this variable will be updated. So, we can pass the whole Video Item.You can refer to the following code. Here, I inherited the
ObservableObject
for the Video model notINotifyPropertyChanged
.Video.cs
MyViewModel.cs
Here, we can get the whole video item by following code:
You can refer to the whole code of MyViewModel:
MyViewModel.cs
3.Usage example:
Note:
I also added a Button to get all the selected video list.