-
May 14th, 2013, 03:55 AM
#1
[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.
-
May 14th, 2013, 04:31 AM
#2
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.
-
May 14th, 2013, 04:37 AM
#3
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.
-
May 14th, 2013, 04:39 AM
#4
Re: MVVM: Populate a view (with listbox) when an item from another view (with listbox
Originally Posted by dee-u
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.
-
May 16th, 2013, 01:33 AM
#5
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>
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|