Hello community,
I'm new to the board and was wondering if someone of you could help me with the following problem.
I've got a ListView which has to be reloaded as soon as a Switch has been toggled.
Great would be something like this:
DataTemplate:
class ViewXOrFilterValue : ViewCell
{
public ViewXOrFilterValue(Action<string> funcUpdateList)
{
StackLayout CellWrapper = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand
};
Switch sw = new Switch()
{
HorizontalOptions = LayoutOptions.EndAndExpand,
};
sw.Toggled += (object sender, ToggledEventArgs e) => {
funcUpdateList("test123123");
};
CellWrapper.Children.Add(sw);
View = CellWrapper;
}
}
And then somewhat like
...
tpXOrFilterValueList.ItemTemplate = new DataTemplate(typeof(views.filter.ViewXOrFilterValue(OWN_METHOD_WHICH_TRIGGERS_RELOAD_OF_LIST)));
tpXOrFilterValueList.ItemsSource = maOrgSelectableValues.ToArray();
...
to set the Template and the Source. But I dont know if its even possible to add parameters to DataTemplates?
I also tried to work with Command Bindings as described here: https://blog.xamarin.com/simplifying-events-with-commanding/
The whole thing works basiaclly but in the end the command which I was binding is always null:
void OnSwitchToggled(object sender, ToggledEventArgs e)
{
if (Command == null)
{
System.Diagnostics.Debug.WriteLine("Command is null!");
return;
}
System.Diagnostics.Debug.WriteLine("EXECUTING calling method....!");
object parameter = Converter.Convert(e, typeof(object), null, null);
if (Command.CanExecute(""))
{
System.Diagnostics.Debug.WriteLine("Executing..");
Command.Execute("");
}
else
{
System.Diagnostics.Debug.WriteLine("Not able to execute!");
}
}
The code for preparing item source / binding is the following:
...
maOrgSelectableValues = new List<DialogXOrValue>();
foreach (var oVal in filter._aValues)
{
DialogXOrValue oXorVal = new DialogXOrValue();
oXorVal._bSelectable = oVal._bSelectable;
oXorVal._bSelected = oVal._bSelected;
oXorVal._iSort = oVal._iSort;
oXorVal._sText = oVal._sText;
oXorVal._sValue = oVal._sValue;
oXorVal.methListViewUpdate = new Command<string>(reloadFilterList);
oXorVal.bIsVisible = true;
maOrgSelectableValues.Add(oXorVal);
}
...
tpXOrFilterValueList.ItemTemplate = new DataTemplate(typeof(views.filter.ViewXOrFilterValue));
tpXOrFilterValueList.ItemsSource = maOrgSelectableValues.ToArray();
...
And the Binding in the Cell:
...
ViewXOrFilterValueSwitchToggleBehavior oBehaviour = new ViewXOrFilterValueSwitchToggleBehavior();
oBehaviour.SetBinding(ViewXOrFilterValueSwitchToggleBehavior.CommandProperty, "methListViewUpdate");
// oBehaviour.SetBinding(ViewXOrFilterValueSwitchToggleBehavior.InputConverterProperty,
sw.Behaviors.Add(oBehaviour);
...
I think the binding of the command is not working because I'm trying to bind it to the switch element?!
Did someone of you had a similar problem?
I'd be happy about any help!
Thanks in advance,
Alex