I am fairly new to mvvm + silverlight concepts and am running into a roadblock. I have a silverlight treecontrol bound to a viewmodel that is refreshed on the click event of a button. My problem is , if for any reason, there is a model binding error, i want to clear the treecontrol and not display anything. Currently, if the model has valid data the first time the button is clicked, the tree populates and if i click the button a second time and due to some problem , the model binding failed, the treecontrol retains its last populated value. I want the behaviour to change so that when there is an error, the treecontrol is cleared , so the UI and model are in sync. I am using Silverlight 3 with CSLA v 3.8.2. Here are my code snippets.
XAML -
<UserControl.Resources>
<ResourceDictionary>
<this:TreeListViewModel x:Key="TreeListModel" />
<common:HierarchicalDataTemplate x:Key="TreeTemplate" ItemsSource="{Binding Path=TreeNodeList}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Id}" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Count}" />
</StackPanel>
</common:HierarchicalDataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<input:AutoCompleteBox x:Name="acbDomain" ItemsSource="{Binding Path=Model}" ValueMemberBinding="{Binding Name}" >
<input:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</input:AutoCompleteBox.ItemTemplate>
</input:AutoCompleteBox>
<Button x:Name="btnSetDomain" Content="Go" Click="btnSetDomain_Click" />
<controls:TreeView x:Name="treeViewCtrl" Style="{StaticResource SimpleTreeView}" DataContext="{StaticResource TreeListModel}" ItemsSource="{Binding Path=Model}"
ItemTemplate="{StaticResource TreeTemplate}" />
Code Behind –
private void btnSetDomain_Click(object sender, RoutedEventArgs e)
{
Guid domainId = ((DomainInfo)acbDomain.SelectedItem).Id; ((TreeListViewModel)this.Resources["TreeListModel"]).GetTreeNodes(domainId);
}
Thanks
Ranjini
If your viewmodel inherits from ViewModel<T> or ViewModelBase<T> (which it doesn't look like yours does) then you'll have a Model property that exposes the model for binding to the UI.
And there are protected methods such as OnError() you can override to detect when an error has occurred. In your OnError() override you could just set Model to null, which will blank the UI.
You can do the same thing in your viewmodel class too - it all depends on how you are handling your data access and error/exception handling in your code.
Thanks much! I was inheriting ViewModel. That was simple! Thanks again.
On separate topic ( I will start a new discussion if I need to), I need to be able to capture fetchcompleted on the UI (through the viewmodel) in order to trigger some events. Is this possible, if so how?
Thanks again
Ranjini
Override OnModelRefreshed().
I was just about to write that I figured it out. I did overide OnModelRefreshed. Thanks again. You have been very helpful.
Hey,
I am new in Silverlight.
Can You please explain how to implement OnModelRefreshed method(full code) in view model.
highly thankfull to you.
thanks
Copyright (c) Marimer LLC