Hi all
I have a ListView
with a TemplateSelector
and two different DataTemplates (ViewCells). Behind the ListView
is a ViewModel for the ConentPage
with an ObservableCollection
which contains some item-models. The Binding to this Item-Model in my templates work like a charm.
What I want: My ContentPage-ViewModel has some commands. The DataTemplate should use this command. How can I bind to this commands?
I know, that I can make the commands static and reference with x:Static
to it. Or I can add the Command to my Item-Model. But both are no Option for my solution.
Thanks in advance
I have found a way to achive this. I made a Basic class for my templates and added a BindableProperty to it:
public class BaseTemplate : ViewCell { public static BindableProperty ParentBindingContextProperty = BindableProperty.Create(nameof(ParentBindingContext), typeof(object), typeof(BaseTemplate), null); public object ParentBindingContext { get { return GetValue(ParentBindingContextProperty); } set { SetValue(ParentBindingContextProperty, value); } } }
And in my TemplateSelector I do the following:
protected override DataTemplate OnSelectTemplate(object item, BindableObject container) { var mytemplate = new DataTemplate(typeof (MyTemplateType)); mytemplate .SetValue(BaseTemplate.ParentBindingContextProperty, container.BindingContext); return mytemplate; }
Now I can Access the parent ViewModel with the property ParentBindingContext
.
Is there a better way to do this?
Answers
I have found a way to achive this. I made a Basic class for my templates and added a BindableProperty to it:
And in my TemplateSelector I do the following:
Now I can Access the parent ViewModel with the property
ParentBindingContext
.Is there a better way to do this?
thanks for @MichaelJhl,you are great!
@MJoehl how can I do it with XAML?
I tried the code below but it didn't work:
It does work, thanks alot!
But I found one downside: You cant use
CachingStrategy="RecycleElement"
on the listview if you use this custom view cellAny thoughts on that?
@MichaelJhl, did you inherit from BaseTemplate for your datatemplates? When I do, I get Error CS0260: Missing partial modifier on declaration of type 'SomeDataTemplate'; another partial declaration of this type exists.
I was able to get this going and it works very well (code behind and xaml), thanks @MichaelJhl for sharing!
Hi !
Thanks for this tip, in theory I'm agree with it... but impossible to make it works on my side :-(
First, in your override "DataTemplate OnSelectTemplate", you can't create a new DataTemplate for each row... because on Android you will reach the "23 template count limit"... and I don't use RecycleElement as @Lippel suggested
So this is my code
'public class CheckItem_DatatemplateSelector : DataTemplateSelector
{
DataTemplate Acceptedtemplate { get; set; }
DataTemplate Refusedtemplate { get; set; }
DataTemplate Uncheckedtemplate { get; set; }
The binding seems to work.
But in my "CkeckItem_accepted" (or in the two other templates too), I try to bind a button with my "ParentBindingContext.m_CallExpCommand" and I don't know why but the command is never executed...
''
Does anybody that has made this tips working, can post a complete sample ? (or help if you see errors...)
Thanks in advance
@RianDutra Insted of ParentBindingContext u should use BindingContext.
Is RecycleElementAndDataTemplate a suitable fix to the issue @Lippel and @Liger_Jerome mention?
"The RecycleElementAndDataTemplate caching strategy builds on the RecycleElement caching strategy by additionally ensuring that when a ListView uses a DataTemplateSelector to select a DataTemplate, DataTemplates are cached by the type of item in the list. Therefore, DataTemplates are selected once per item type, instead of once per item instance."
@UnreachableCode
Quite some time passed since I added my comment.
I believe RecycleElementAndDataTemplate didnt exist at that time. You should give it a try, it seems very promising.
Hum... yes there was some time passed ...
But I remember that I finally got this working (and this is in production since months)
Opening the source-code (my memory is failing), I can see that :
If you want some sample code, MP-me
Hm, I'm getting a pretty big performance hit when set to RecycleElementAndDataTemplate. I'm not sure I can use this. Will I have problems if I leave it set to RetainElement?
Hey Folks,
Thank you very much for your update here
I was stuck by too days to get call to command in from a partial class to public class TheClassViewModel : BindableObject
So with your bread sticks here I could made it work.
So, here is the code:
public partial class TheClassCellOption : ViewCell
{
}
Here is the selection class
public class TheClassDataTemplateSelector : DataTemplateSelector
{
private readonly DataTemplate TheClassCellOption;
private readonly DataTemplate OtherClassCellOption;
the view model class
.
.
.
.
.
.
}