Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello.
I was trying with success building a grid on SL4 by XAML (on datagrid control)
like this :

DataGridColumn gt;
DataTemplate dt;
StringBuilder CellTemp = new StringBuilder();
CellTemp.Append("<DataTemplate ");
CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
CellTemp.Append("2006/xaml/presentation' ");
CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
CellTemp.Append("<TextBox ");
CellTemp.Append("Text='{Binding myElement, Mode=TwoWay}'></TextBox> ");
CellTemp.Append("</DataTemplate>");
gt = new DataGridTemplateColumn();
dt = (DataTemplate)XamlReader.Load(CellTemp.ToString());
((DataGridTemplateColumn)(gt)).CellTemplate = dt;

Also adding events for KeyDown like on the forum :
http://forums.silverlight.net/forums/p/191624/442641.aspx

But I cannot capture, for some reason CellEditEnded & RowEditEnded events (the cells are auto - editing).
I think not doing the cells auto-edit, but doing this on some other trickery, like : CurrentCellChanged on the grid & doing this.BeginEdit();

But, finally - how can I capture the CellEditEnded & RowEditEnded events, and can I eliminate the auto-edit behaviour of DataGrid cells when adding them by XAML dynamically (DataGridTemplateColumn on code).

Thanks. :).
Posted
Updated 27-Apr-11 12:36pm
v2
Comments
HimanshuJoshi 27-Apr-11 18:36pm    
Edited to add pre tags around code.

1 solution

This is happening because you have specified CellTemplate as TextBox which will never bring your grid to editmode.
(This is why you cells are auto editing.)

You can Use TextBlock for CellTemplate and TextBox for CellEditingTemplate. Something similar to below.

This will enable your grid to trigger event(s) required.

XML
DataGridColumn gt;
            DataTemplate dt, dtEdit;
            StringBuilder CellTemp = new StringBuilder();
            CellTemp.Append("<DataTemplate ");
            CellTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
            CellTemp.Append("2006/xaml/presentation' ");
            CellTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");
            CellTemp.Append("<TextBlock ");
            CellTemp.Append("Text='{Binding myElement, Mode=TwoWay}'></TextBlock> ");
            CellTemp.Append("</DataTemplate>");

            StringBuilder CellEditTemp = new StringBuilder();
            CellEditTemp.Append("<DataTemplate ");
            CellEditTemp.Append("xmlns='http://schemas.microsoft.com/winfx/");
            CellEditTemp.Append("2006/xaml/presentation' ");
            CellEditTemp.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");
            CellEditTemp.Append("<TextBox ");
            CellEditTemp.Append("Text='{Binding myElement, Mode=TwoWay}'></TextBox> ");
            CellEditTemp.Append("</DataTemplate>");
            gt = new DataGridTemplateColumn();
            gt.Header = "Some Text";
            dt = (DataTemplate)XamlReader.Load(CellTemp.ToString());
            dtEdit = (DataTemplate)XamlReader.Load(CellEditTemp.ToString());
            ((DataGridTemplateColumn)(gt)).CellTemplate = dt;
            ((DataGridTemplateColumn)(gt)).CellEditingTemplate = dtEdit;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900