代码之家  ›  专栏  ›  技术社区  ›  BuddyJoe

Silverlight、Caliburn、操作和数据报

  •  7
  • BuddyJoe  · 技术社区  · 14 年前

    有人知道Caliburn或Caliburn Micro框架示例的好代码示例,这些示例演示了使用DataGrid项的路由操作吗?

    1 回复  |  直到 14 年前
        1
  •  11
  •   Christopher Bennage    14 年前

    此示例将操作附加到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