Binding using DataTemplate based on Type of SelectedItem

6 posts, 0 answers
  1. Heather
    Heather avatar
    54 posts
    Member since:
    Sep 2010

    Posted 23 Sep 2010 Link to this post

    I am evaluating Telerik for use in a WPF 4.0 app I am creating.  So far most of the replacement of MS controls has gone smoothly but I am struggling with the tabcontrol some.  I am using MVVM.  I have a view model which has a collection of other viewmodels.  My tabcontrol is bound to the collection of viewmodels.  I want my tabcontrol to show its content based on the viewmodel created/selected.  I have DataTemplates set up that are tied to the class type of the viewmodels in the collection.  This works great with the MS Tabcontrol but does not seem to work with the RadTabControl.  Does the RadTabControl only support one type of content?  My view collection starts out with two items in it, a DeviceInventoryListViewModel and an AccountMaintenanceListViewModel, the DeviceInventoryList view is shown correctly as the initial tab content but when I click on the Account tab it tries to coerce the contents of the AccountListViewModel into the Device view rather than creating new content with the AccountMaintenanceList view and I get an error about Accounts not being able to be set to Devices (or something similar).

    I have the following set up in my Application.xaml:
    <P><DataTemplate DataType="{x:Type vm:DeviceInventoryListViewModel}"
    <BR> <local:DeviceInventoryList/> <BR></DataTemplate
    <BR><DataTemplate DataType="{x:Type vm:AccountMaintenanceListViewModel}"
    <BR> <local:AccountMaintenanceList /> <BR></DataTemplate></P>

    My ShellViewModel is as follows:
    Public Class ShellViewModel
        Inherits ViewModelBase

        Private _openviews As ObservableCollection(Of ViewModelBase)
        Private _currentview As ViewModelBase

        Public ReadOnly Property Views As ObservableCollection(Of ViewModelBase)
            Get
                If _openviews Is Nothing Then
                    _openviews = New ObservableCollection(Of ViewModelBase)
                     AddHandler _openviews.CollectionChanged, AddressOf ViewCollectionChanged
                     LoadDefaultViews()
                End If
                Return _openviews
            End Get
        End Property
     
        Private Sub LoadDefaultViews()
            _openviews.Add(New DeviceInventoryListViewModel)
            _openviews.Add(New AccountMaintenanceListViewModel)
            _currentview = _openviews.Item(0)
        End Sub

     
       Public Property SelectedView As ViewModelBase
             Get
                 Return _currentview
             End Get
             Set(ByVal value As ViewModelBase)
                _currentview = value
                OnPropertyChanged("SelectedView")
            End Set
        End Property
    End Class

    And then my ShellView has its DataContext set to an instance of my ShellViewmodel and the XAML contains the following:

    <telerik:RadTabControl Grid.Row="1" ItemsSource="{Binding Views}"  
        SelectedItem="{Binding SelectedView}" />
  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 28 Sep 2010 Link to this post

    Hello Heather,

    I am not sure that I fully understand the nature of the problem. I've tried replicating your scenario and I did not receive any errors. Could you please take a look at the attached sample project and tell me if I am missing something. I'd be glad to further assist you.

    Kind regards,
    Kiril Stanoev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Heather
    Heather avatar
    54 posts
    Member since:
    Sep 2010

    Posted 29 Sep 2010 Link to this post

    I also finally got it to work with a more simple example, now have to go back and figure out why my full featured attempt errored out.  I will update shortly if I can figure out what the gottcha is.

    One thing I am noticing is that the grids contained within the tab items does not seem to maintain certain "current states" when switching between tabs.  It seems to keep the sort order and group definitions but not settings within the groups like which group(s) are expanded or the sort order of the groups.  Is this something that can be fixed?

    I am also exploring using prism regions instead of having the tabcontrol binding to a collection and using data templates.  When I do that the grids maintain their "state" between switching tabs BUT I encountered another issue (which I posted yesterday) in regards to having problems with activating an existing tab via region.Activate(view).
  5. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 04 Oct 2010 Link to this post

    Hello Heather,

    One thing I am noticing is that the grids contained within the tab items does not seem to maintain certain "current states" when switching between tabs.  It seems to keep the sort order and group definitions but not settings within the groups like which group(s) are expanded or the sort order of the groups.  Is this something that can be fixed?

    I modified our Aggregates QSF example and wrapped the GridView in a TabItem. Then I played with the grouping and I did not experience any issues i.e. switching between tabs did not produce any wrong behavior. Have a look at the recorded screencast and let me know if I am doing anything wrong.

    Greetings,
    Kiril Stanoev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Heather
    Heather avatar
    54 posts
    Member since:
    Sep 2010

    Posted 04 Oct 2010 Link to this post

    The difference is I was using DataTemplates, not just embedding the grid in the tabs.  So...datatemplate driven tab creation, two views (for your example), each with a gridview containing lists of different objects.
  7. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 07 Oct 2010 Link to this post

    Hello Heather,

    Thank you for the clarification. Unfortunately, this scenario isn't supported by the RadTabContol for now. The reason is that by design the RadTabControl has only one ContentPresenter which holds the currently selected RadTabItem. And whenever the selection changes, the unselected RadTabItem is removed from the VisualTree and the selected RadTabItem's content is displayed in the ContentPresenter thus its content does not keep its state.

    However, we have logged this issue as a feature request in our PITS where you will be able to track its progress and vote for it thus increasing its priority.

    As a workaround, you can keep the state of the RadTabItem's content in a ViewModel. You can also have a look at this forum thread, I hope it helps.

    Greetings,
    Tina Stancheva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready