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
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:
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
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:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
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
>
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;
}
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