此示例将操作附加到DataGrid中的每一行。该操作在作为整个视图的DataContext的ViewModel上处理。这是在micro中构建的,但语法是相同的。这不使用基于约定的数据绑定。
视图的相关部分是:
<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>
相应的视图模型如下:
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; }
}
此处讨论特殊参数$dataContext:
http://caliburn.codeplex.com/wikipage?title=Parameters&referringTitle=Documentation