LoadOnDemand in MVVM Pattern

4 posts, 0 answers
  1. Andrea Ferrari
    Andrea Ferrari avatar
    9 posts
    Member since:
    Apr 2010

    Posted 10 Jul 2010 Link to this post

    Hi.
    There's a way to Bind the LoadOnDemand Event to a Command in a MVVM Pattern.
    I'm using Galasoft MVVM Light in my application and I load the data from WCF services. All is ok, but now I've to manage the LoadOnDemand Event without breaking the MVVM pattern.

    I've tryied using the EventToCommand of Galasoft and its works fine. This is the XAML code
    <UserControl 
      x:Class="FB_BridgeArchive.View.JobsView"
      xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
      xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
      xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"
      mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="400"
      DataContext="{Binding Jobs, Source={StaticResource Locator}}">
      
      <UserControl.Resources>
        <DataTemplate x:Key="CategoryTemplate">
          <TextBlock Text="{Binding Description}" />
        </DataTemplate>
        <HierarchicalDataTemplate x:Key="LevelTemplate" ItemsSource="{Binding Categories}" ItemTemplate="{StaticResource CategoryTemplate}">
          <TextBlock Text="{Binding Description}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="TeacherTemplate" ItemsSource="{Binding Levels}" ItemTemplate="{StaticResource LevelTemplate}">
          <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
      </UserControl.Resources>
        
      <StackPanel Orientation="Vertical">
        <telerik:RadTreeView x:Name="jobTreeView" Margin="8" ExpanderStyle="{StaticResource ExpanderStyle}"
                             IsLoadOnDemandEnabled="True" IsExpandOnSingleClickEnabled="True"
                             ItemTemplate="{StaticResource TeacherTemplate}" ItemsSource="{Binding Path=Ds.Teachers}">
        <i:Interaction.Triggers>
          <i:EventTrigger EventName="LoadOnDemand">
            <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding LoadOnDemand}" />
          </i:EventTrigger>
        </i:Interaction.Triggers>
      </telerik:RadTreeView>
      </StackPanel>
    </UserControl>
    As you can see I call the LoadOnDemand Command in my ViewModel.
    Now, in the ViewModel I've the followind method to handle the LoadOnDemand:
    private void HandleLoadOnDemand(RadRoutedEventArgs e)
    {
      RadTreeViewItem item = e.OriginalSource as RadTreeViewItem;
      if (item.Item.GetType() == typeof(WcfTeacher))
      {
        Ds.LoadLevels(item.Item as WcfTeacher);
      }
      else if (item.Item.GetType() == typeof(WcfLevel))
      {
        Ds.LoadCategories(item.Item as WcfLevel);
      }
      else
        item.IsLoadOnDemandEnabled = false;
    }
    It works, but with a problem.
    When you click on an item, the method is called and the items are loaded, but you don't see them on the TreeView. You need to click on the item one more time in order to see the item expanded.
    Why? 
    I've forgotten something, or there's some other problems?
    Maybe it's the wrong way to bind the LoadOnDemand event, but there's a better way? 

    Thanks a lot and regards.
    Andrea
  2. Miro Miroslavov
    Admin
    Miro Miroslavov avatar
    588 posts

    Posted 15 Jul 2010 Link to this post

    Hello Andrea Ferrari,

    Can you please send us example project, so we will be able to give you more detailed information about the problem - you can create support ticket and attach the project.

    On a side note in my opinion - using RadTreeViewItem (View class) in the ViewModel class breaks much more the MVVM pattern than having handler in the View that do the same job (mainly View stuff).


    Sincerely yours,
    Miro Miroslavov
    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. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 26 Aug 2010 Link to this post

    Hello Joey,

    I replied in the mentioned thread.

    Regards,
    Miroslav
    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