In the Application Resources that you can see below I’ve defined two DataTemplates. Protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) Protected override DataTemplate SelectTemplateCore(object item) public class DetailViewTemplateSelector:DataTemplateSelector After the name of the item was grabbed, the code below looks into the Application’s Resources for a DataTemplate that is stored under a key that matches exactly the ViewModel’s name. So the item is either a FriendDetailViewModel or a BookDetailViewModel. That item is in case of my application a ViewModel, as I want to use this selector on the Pivot. In the SelectTemplateCore-method it takes the name of the received item. I’ve implemented a class called DetailViewTemplateSelector. That means you have to implement a subclass of DataTemplateSelector and resolve the correct DataTemplate on your own. Now I would be done!īut unfortunately, UWP does not support implicit DataTemplates. Then the Pivot would automatically grab the correct View for the corresponding ViewModel. Now if we would have implicit DataTemplates, I could do this on my Mainpage: I have also the corresponding views for these viewModels. Two classes inherit from DetailViewModelBase in my app: When you look at the Details-property of the MainViewModel, you can see that the items in that ObservableCollection are of type DetailViewModelBase. Ok, it’s not fair to blame the Pivot for this, but anyway, it was one of the main issues I had. Implicit DataTemplates don’t exist in UWP. ![]() The harder parts I’ve identified where these: But overall, the Pivot is quite powerful and gets pretty close to what I expect from a TabControl. Some parts didn’t work as I would expect them to work. Now let’s look at the parts that felt not perfect to me (Can a framework be perfect? :)) I also set the HeaderTemplate to define the header with the text and the close button. Public DetailViewModelBase SelectedDetailĪnd in XAML, I use the Pivot’s ItemsSource-property and SelectedItem-property to bind to the MainViewModel‘s properties, as you can see below. Private DetailViewModelBase _selectedDetail In the MainViewModel I’ve defined the properties Details and SelectedDetail like below (Note: this is not the full-blown MainViewModel): public class MainViewModel : ViewModelBase Now let’s look at some code parts that are quite common to TabControl users. ![]() If the friend has been changed, this popup appears when the user tries to close the tab: In the ViewModel for a friend I check whether the friend has been changed by the user or not. When you click on the little X-button in the tab header, the tab closes… did I say “tab”? Of course I mean pivot-item. ![]() Clicking on “Open new friend” or “Open new book” opens up a new tab and also adds that item to the navigation on the left. There was not everything perfect, but let’s look at these “non-perfect”-points later and let’s start with the sample.īelow you see a screenshot of the sample-application. And again, UWP felt like a pretty amazing technology. ![]()
0 Comments
Leave a Reply. |