WPF DataTemplateSelector Question
-
I have a Contacts control. On the left side is the list of contacts. I created two DataTempates for the list items in the Control's resources. Above the list are two buttons, List View and Card View. When they are selected, I want to change the data template of the list items accordingly. Here's a picture of Outlook Contacts showing the People button selected above the list. When one of the Current View buttons is selected, the list box items look different. This is what I'm looking for: [https://www.brycematheson.io/wp-content/uploads/2018/04/Image1\_Blurred-1024x713.jpg\](https://www.brycematheson.io/wp-content/uploads/2018/04/Image1\_Blurred-1024x713.jpg) I have a DataTemplateSelector, but when it' called the container property is the ListItem. How do I know in the DataTemplateSelector what view mode was selected in the ViewModel? By DataTemplateSelector
public class ContactViewDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
FrameworkElement element = container as FrameworkElement;ContactsView view = container as ContactsView; //< View is Null at this point switch (view.ViewMode) { case ContactsView.ViewModes.Card: return element.FindResource("cardViewTemplate") as DataTemplate; break; case ContactsView.ViewModes.List: return element.FindResource("listViewTemplate") as DataTemplate; break; } return null; }
}
DataTemplates
-
I have a Contacts control. On the left side is the list of contacts. I created two DataTempates for the list items in the Control's resources. Above the list are two buttons, List View and Card View. When they are selected, I want to change the data template of the list items accordingly. Here's a picture of Outlook Contacts showing the People button selected above the list. When one of the Current View buttons is selected, the list box items look different. This is what I'm looking for: [https://www.brycematheson.io/wp-content/uploads/2018/04/Image1\_Blurred-1024x713.jpg\](https://www.brycematheson.io/wp-content/uploads/2018/04/Image1\_Blurred-1024x713.jpg) I have a DataTemplateSelector, but when it' called the container property is the ListItem. How do I know in the DataTemplateSelector what view mode was selected in the ViewModel? By DataTemplateSelector
public class ContactViewDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
FrameworkElement element = container as FrameworkElement;ContactsView view = container as ContactsView; //< View is Null at this point switch (view.ViewMode) { case ContactsView.ViewModes.Card: return element.FindResource("cardViewTemplate") as DataTemplate; break; case ContactsView.ViewModes.List: return element.FindResource("listViewTemplate") as DataTemplate; break; } return null; }
}
DataTemplates
You've got the
ListItem
; you just need to use something like this function[^] to walk up the tree to find the control, and grab itsDataContext
property, which should be yourContactsView
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You've got the
ListItem
; you just need to use something like this function[^] to walk up the tree to find the control, and grab itsDataContext
property, which should be yourContactsView
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
The problem is that the DataTemplateSelector gives me the ContactEntity, not the ListItem
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
You've got the
ListItem
; you just need to use something like this function[^] to walk up the tree to find the control, and grab itsDataContext
property, which should be yourContactsView
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
That did it. Here's what I came up with:
public static class VisualTreeExtensions
{
public static T FindParentOfType(this DependencyObject child) where T : DependencyObject
{
DependencyObject parentDepObj = child;
do
{
parentDepObj = VisualTreeHelper.GetParent(parentDepObj);
T parent = parentDepObj as T;
if (parent != null) return parent;
}
while (parentDepObj != null);
return null;
}
}If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.