Select to view content in your preferred language

How to bring back an value of a cell in datagrid (Silverlight)

687
1
02-13-2014 01:54 AM
Abdoulaye_Djibril_MademouDIALL
Emerging Contributor
Hello everyOne,
I have a difficult with the datagrid in silverlight,
In fact i am looking for but without success
how to bring back  the value of a cell  in a datagrid in silverlight after a click on a row into a datagrid

Any Help would be beneficial
Thanks in advance
0 Kudos
1 Reply
BrentHoskisson
Frequent Contributor
Give your column a template like this:

         ....

                <sdk:DataGridTemplateColumn Header="Site Address" SortMemberPath="SortableAddress">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding FormattedAddress}" VerticalAlignment="Center"  />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>  

        .....              




Then I call the following in the appropriate ButtonUp event:



        private void MyDataGridName_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            SelectDataRow(MyDataGridName, e.GetPosition(Application.Current.RootVisual));
            ..........
        }

        private void SelectDataRow(DataGrid dg, Point loc)
        {
             //Finds every control in the datagrid at the location
            var list = VisualTreeHelper.FindElementsInHostCoordinates(loc, dg);
            if (list == null || list.Count() == 0) return;
            var row = list.FirstOrDefault(el => el is DataGridRow) as DataGridRow;
            if (row != null)
            {
                dg.SelectedIndex = row.GetIndex();  // make that row selected
                //  Find the text block at the location you clicked...
                var cellContent = list.FirstOrDefault(el => el is TextBlock) as TextBlock;
                //Sets the text to be grabbed by the clipboard...  (or whatever you want to do with it)
                _copyText = (cellContent == null) ? "" : cellContent.Text;
            }
            else
            {
                //It wasn't a row, it was a header - select the header column...
                var colHeader = list.FirstOrDefault(el => el is System.Windows.Controls.Primitives.DataGridColumnHeader) as System.Windows.Controls.Primitives.DataGridColumnHeader;
            }
        }
0 Kudos