Results 1 to 5 of 5

Thread: [RESOLVED] MVVM: Populate a view (with listbox) when an item from another view (with listbox) is

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Resolved [RESOLVED] MVVM: Populate a view (with listbox) when an item from another view (with listbox) is

    I have a list of Topics, and for each topic there are some Examples. What I want to be able to do is that when a topic is selected then I will populate another view with all Examples for that selected topic. I'm still learning MVVM so not sure how to implement such.

    This is what I've got so far but I haven't figured out yet how to receive/set the _exampleViewModel via binding in XAML.
    Code:
    public TopicViewModel SelectedTopic
        {
            get { return _selectedTopic; }
            set
            {
                _selectedTopic = value;
                OnPropertyChanged("SelectedTopic");
    
                //refresh list of exercises
                if (_exampleViewModel != null)
                {
                    _exampleViewModel.RefreshExercises(_selectedTopic.ID);
                }
            }
        }
    Last edited by dee-u; May 14th, 2013 at 04:05 AM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: MVVM: Populate a view (with listbox) when an item from another view (with listbox

    Why is this question in the VB.NET forum when it's C# code (there's a C# forum) for a WPF application (there's a WPF forum)? I have asked the mods to move it to a more appropriate location. MVVM questions belong in the WPF forum because MVVM is specifically a WPF design pattern.

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: MVVM: Populate a view (with listbox) when an item from another view (with listbox

    I've seen some threads here that talked about MVVM so I tried asking here but forgot to convert my code.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: MVVM: Populate a view (with listbox) when an item from another view (with listbox

    Quote Originally Posted by dee-u View Post
    I've seen some threads here that talked about MVVM so I tried asking here but forgot to convert my code.
    It should be in WPF because that's where the people with MVVM experience are. Even if your code was VB, most people who've used MVVM with C# would still be able to answer the question because it's about MVVM and not about VB.NET.

  5. #5

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: MVVM: Populate a view (with listbox) when an item from another view (with listbox

    Got it working by exposing a ViewModel to be consumed by the Views. This is my 'parent' ViewModel (stripped unnecessary codes) and as you can see there is the ExerciseVM and Example VM in there:
    Code:
    namespace WorkText.ViewModel
    {
        public class AllTopicsViewModel : WorkspaceViewModel
        {
            private TopicViewModel _selectedTopic = null;
            public TopicViewModel SelectedTopic
            {
                get { return _selectedTopic; }
                set
                {
                    _selectedTopic = value;
    
                    OnPropertyChanged("SelectedTopic");
    
                    this.ExerciseVM = new AllExercisesViewModel(value);
                    this.ExampleVM = new AllExamplesViewModel(value);
                }
            }
    
            private AllExercisesViewModel _exerciseViewModel = null;
            public AllExercisesViewModel ExerciseVM
            {
                get
                {
                    return _exerciseViewModel;
                }
                set
                {
                    if (_exerciseViewModel != value)
                    {
                        _exerciseViewModel = value;
                        base.OnPropertyChanged("ExerciseVM");
                    }
                }
            }
    
            private AllExamplesViewModel _exampleViewModel = null;
            public AllExamplesViewModel ExampleVM
            {
                get
                {
                    return _exampleViewModel;
                }
                set
                {
                    if (_exampleViewModel != value)
                    {
                        _exampleViewModel = value;
                        base.OnPropertyChanged("ExampleVM");
                    }
                }
            }
        }
    }
    And this is the ViewModel of Exercises:
    Code:
    namespace WorkText.ViewModel
    {
        public class AllExercisesViewModel : ViewModelBase
        {
            public OptimizedObservableCollection<ExerciseViewModel> AllExercises { get; private set; }
    
            public AllExercisesViewModel(TopicViewModel topic)
            {
                AllExercises = new OptimizedObservableCollection<ExerciseViewModel>();
    
                _exerciseRepository = new ExerciseRepository();
    
                List<ExerciseViewModel> all = (from cust in _exerciseRepository.GetExercises(topic.ID)
                                               select new ExerciseViewModel(cust, _exerciseRepository)).ToList();
    
                AllExercises.AddRange(all);
                this.AllExercises.CollectionChanged += this.OnCollectionChanged;
            }
        }
    }
    And this is how I bind them in XAML.
    Code:
    <Window x:Class="WorkText.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WorkText"
          
            Title="Worktext" Height="545" Width="926" SnapsToDevicePixels="False" WindowState="Maximized" WindowStyle="SingleBorderWindow" WindowStartupLocation="CenterScreen" Icon="/WorkText;component/Images/Extensions.ico" Closing="Window_Closing" Loaded="Window_Loaded" xmlns:my="clr-namespace:WorkText.View">
            
        <Window.Resources>
            <ResourceDictionary Source="MainWindowResources.xaml" />        
        </Window.Resources>
    
        <!--Set this window's DataContext to AllTopicViewList so that TopicsView and ExerciseView will be able to bind-->
        <Window.DataContext>
            <Binding Source="{StaticResource TopicList}"/>
        </Window.DataContext>
        
        <Grid>            
                    <my:ExamplesView DataContext="{Binding ExampleVM, Mode=OneWay}" HorizontalAlignment="Stretch" x:Name="examplesView" VerticalAlignment="Stretch"  />
                    <my:ExercisesView DataContext="{Binding ExerciseVM, Mode=OneWay}" HorizontalAlignment="Stretch" x:Name="exercisesView" VerticalAlignment="Stretch"  />
                	<my:TopicsView Grid.Row="1" Grid.RowSpan="2" HorizontalAlignment="Stretch" x:Name="topicsView" VerticalAlignment="Stretch" />            
            </Grid>
        </Grid>
    </Window>
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width