2010-10-14 41 views

回答

11

此示例附着到在數據網格的每一行的動作。該操作在作爲整個視圖的datacontext的視圖模型上處理。這是建立在微型,但語法是相同的。這不使用基於約定的數據綁定。

視圖的相關部分是:

<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