This is a migrated thread and some comments may be shown as answers.

Finding an element in a GridViewColumn CellTemplate

4 Answers 571 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Adam Clark
Top achievements
Rank 1
Adam Clark asked on 13 Sep 2010, 08:54 PM
It is necessary to find a specific named element in the CellTemplate of a GridViewColumn.

There is a GridViewColumn defined like this:

<GridViewColumn Header="column 1">
     <GridViewColumn.CellTemplate>
          <DataTemplate>
               <TextBox x:Name="txtcontent"/>
          </DataTemplate>
     </GridViewColumn.CellTemplate>
</GridViewColumn>

It is possible to find an element in a DataTemplate by getting the ContentPresenter where the DataTemplate is applied, and using the FindName method of the DataTemplate to find a specific element.

DataTemplate celltemplate = column.CellTemplate;
ContentPresenter contentpresenter = ...
TextBox txtbox = (TextBox)celltemplate.FindName("txtcontent", contentpresenter);

How can you find the ContentPresenter where the GridViewColumn CellTemplate is applied?

4 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 14 Sep 2010, 06:40 AM
Hello Adam Clark,

There are several possible approaches which can be used depending on the exact scenario. When should this element be retrieved? On mouse lick or when some other event has occurred?


All the best,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Adam Clark
Top achievements
Rank 1
answered on 14 Sep 2010, 02:47 PM
In this scenario, a RadGridView is used to present data in a FlowDocument.  Properties of the RadGridView and it's columns are designed by the user to control how the data looks when the document is printed.  When a specific custom property of a column changes, this element should be retrieved.

public class CustomGridViewColumn : GridViewColumn
{
     private string _customString;
     public string CustomString
     {
          get { return _customString; }
          set
          {
               _customString = value;
               UpdateCellTemplate();
          }
     }
 
     private void UpdateCellTemplate()
     {
          // retrieve the element
     }
}
0
Milan
Telerik team
answered on 17 Sep 2010, 10:23 AM
Hello Adam Clark,

One possible solution is to define CustomString as DependencyProperty and bind the property of your display element to that DependencyProperty. For example:

public class CustomColumn : GridViewDataColumn
{
    public double Size
    {
        get { return (double) GetValue(SizeProperty); }
        set { SetValue(SizeProperty, value); }
    }
  
    public static readonly DependencyProperty SizeProperty =
        DependencyProperty.Register("Size", typeof(double), typeof(CustomColumn), new PropertyMetadata(10.0));
  
    public override System.Windows.FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
    {
        var element = base.CreateCellElement(cell, dataItem) as TextBlock;
  
        Binding b = new Binding("Size");
        b.Mode = BindingMode.TwoWay;
        b.Source = this;
  
        element.SetBinding(TextBlock.FontSizeProperty, b);
  
        return element;
    }
}

That way the FontSize of every displayed TextBlock will change when the Size property of the column is changed.

Hope this helps.


All the best,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Adam Clark
Top achievements
Rank 1
answered on 27 Sep 2010, 05:25 PM
Give yourself some telerik points for this response.  Thank you, your solution is perfect.
Tags
GridView
Asked by
Adam Clark
Top achievements
Rank 1
Answers by
Milan
Telerik team
Adam Clark
Top achievements
Rank 1
Share this question
or