Silverlight, Caliburn, Actions and DataGrids
Asked Answered
R

1

7

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

Rissa answered 14/10, 2010 at 19:48 Comment(1)
You should mark the answer, to provide feedback for the rest of the community.Phrenic
H
11

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

Hulse answered 15/10, 2010 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.