Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight, Caliburn, Actions and DataGrids

Anyone know of good code examples of the Caliburn or Caliburn Micro framework example that illustrate routing Actions with DataGrid items?

like image 664
BuddyJoe Avatar asked Oct 14 '10 19:10

BuddyJoe


1 Answers

This example attaches an action to each row in the datagrid. The action is handled on the viewmodel that is the datacontext for the entire view. This was built in Micro, but the syntax is the same. This does not use the convention-based data binding.

The relevant portion of the view is:

<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 viewmodel 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

like image 154
Christopher Bennage Avatar answered Oct 23 '22 08:10

Christopher Bennage