I’m having a look at this MVVM stuff and I’m facing a problem.
The situation is pretty simple.
I have the following code in my index.xaml page
<Grid> <ItemsControl ItemsSource='{Binding}'> <ItemsControl.ItemTemplate> <DataTemplate> <view:MovieView ></view:MovieView> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Grid>
and in my index.xaml.cs
…
InitializeComponent(); base.DataContext = new MovieViewModel(ent.Movies.ToList()); ….
and here is my MoveViewModel
public class MovieViewModel { readonly List<Movies> _m; public ICommand TestCommand { get; set; } public MovieViewModel(List<Movies> m) { this.TestCommand = new TestCommand(this); _m = m; } public List<Movies> lm { get { return _m; } } }
finally
here is my control xaml MovieView
<Grid> <Grid.RowDefinitions> <RowDefinition Height='Auto' /> <RowDefinition Height='Auto' /> <RowDefinition Height='Auto' /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width='Auto'></ColumnDefinition> <ColumnDefinition Width='Auto'></ColumnDefinition> </Grid.ColumnDefinitions> <Label VerticalAlignment='Center' Grid.Row='0' Grid.Column='0'>Title :</Label><TextBlock VerticalAlignment='Center' Grid.Row='0' Grid.Column='1' Text='{Binding Title}'></TextBlock> <Label VerticalAlignment='Center' Grid.Row='1' Grid.Column='0'>Director :</Label><TextBlock VerticalAlignment='Center' Grid.Row='1' Grid.Column='1' Text='{Binding Director}'></TextBlock> <Button Grid.Row='2' Height='20' Command='{Binding Path=TestCommand}' Content='Edit' Margin='0,4,5,4' VerticalAlignment='Stretch' FontSize='10'/> </Grid>
So the problem I have is that if I set ItemsSource at Binding
it doesn’t make anything
if I set ItemsSource='{Binding lm}’
it populates my itemsControl but the Command (Command='{Binding Path=TestCommand}’ ) doesn’t not work.
Of course it doesn’t not work because TestCommand doesn’t not belong to my entity object Movies.
So finally my question is,
what do I need to pass to the ItemsControl to make it working?
Thx in advance
Got it working
here is the thing
so the RelativeSource was the thing I’ve missed.
if somebody has a good explaination of this, I would be definitely happy.