Silverlight,Caliburn,Actions和DataGrids

时间:2010-10-14 19:48:26

标签: silverlight datagrid action caliburn caliburn.micro

任何人都知道使用DataGrid项目说明路由操作的Caliburn或Caliburn Micro框架示例的良好代码示例?

1 个答案:

答案 0 :(得分:11)

此示例将操作附加到数据网格中的每一行。该操作在viewmodel上处理,viewmodel是整个视图的datacontext。这是用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>

,相应的viewmodel如下所示:

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