This example takes an action for each row in the datagrid. The action is processed on the viewmodel, which is the datacontext for the entire view. It was built into Micro, but the syntax is the same. This does not use convention-based data binding.
Relevant part of the submission:
<sdk:DataGrid ItemsSource="{Binding Source}" AutoGenerateColumns="False"> <sdk:DataGrid.Columns> <sdk:DataGridTemplateColumn Header="Action"> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Do!" cm:Message.Attach="Remove($dataContext)" /> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate> </sdk:DataGridTemplateColumn> <sdk:DataGridTextColumn Binding="{Binding Text}" /> <sdk:DataGridTextColumn Binding="{Binding More}" /> <sdk:DataGridTextColumn Binding="{Binding Stuff}" /> </sdk:DataGrid.Columns> </sdk:DataGrid>
and the corresponding view model looks like this:
public class ShellViewModel : IShell { public ShellViewModel() { Source = new ObservableCollection<MyRow>( new[] { new MyRow {Text = "A1", More = "B", Stuff = "C"}, new MyRow {Text = "A2", More = "B", Stuff = "C"}, new MyRow {Text = "A3", More = "B", Stuff = "C"}, new MyRow {Text = "A4", More = "B", Stuff = "C"}, new MyRow {Text = "A5", More = "B", Stuff = "C"}, } ); } public void Remove(MyRow row) { Source.Remove(row); } public ObservableCollection<MyRow> Source { get; set; } } public class MyRow { public string Text { get; set; } public string More { get; set; } public string Stuff { get; set; } }
The special parameter $ dataContext is discussed here: http://caliburn.codeplex.com/wikipage?title=Parameters&referringTitle=Documentation
Christopher bennage
source share