This is a migrated thread and some comments may be shown as answers.

LoadOnDemand in MVVM Pattern

3 Answers 309 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Andrea Ferrari
Top achievements
Rank 1
Andrea Ferrari asked on 10 Jul 2010, 10:13 AM
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

3 Answers, 1 is accepted

Sort by
0
Miro Miroslavov
Telerik team
answered on 15 Jul 2010, 07:51 AM
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
0
StrandedPirate
Top achievements
Rank 1
answered on 24 Aug 2010, 01:02 AM
0
Miroslav
Telerik team
answered on 26 Aug 2010, 05:35 PM
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
Tags
TreeView
Asked by
Andrea Ferrari
Top achievements
Rank 1
Answers by
Miro Miroslavov
Telerik team
StrandedPirate
Top achievements
Rank 1
Miroslav
Telerik team
Share this question
or