CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum


CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • MVVM Silverlight clear control on error

    rated by 0 users
    Answered (Verified) This post has 2 verified answers | 5 Replies | 3 Followers

    Top 150 Contributor
    60 Posts
    Ranjini posted on Wed, Mar 24 2010 10:17 AM

     

    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

    Ranjini Suryanarayanan

    Answered (Verified) Verified Answer

    Top 10 Contributor
    9,475 Posts
    Verified by 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.

    Rocky

    Top 10 Contributor
    9,475 Posts
    Verified by Ranjini

    Override OnModelRefreshed().

    Rocky

    All Replies

    Top 10 Contributor
    9,475 Posts
    Verified by 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.

    Rocky

    Top 150 Contributor
    60 Posts

    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

    Ranjini Suryanarayanan

    Top 10 Contributor
    9,475 Posts
    Verified by Ranjini

    Override OnModelRefreshed().

    Rocky

    Top 150 Contributor
    60 Posts

    I was just about to write that I figured it out. I did overide OnModelRefreshed. Thanks again. You have been very helpful.

    Ranjini Suryanarayanan

    Not Ranked
    1 Posts
    pooja replied on Fri, Jun 17 2011 11:02 AM

    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

    Page 1 of 1 (6 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems