Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you can use <a href="http://forums.silverlight.net/forums/p/55369/142519.aspx" rel="nofollow">this</a></p> <p>Using this code you can found ancestor of the DataGridColumn - your DataGrid in visual tree. This code implemented as static function but you can change it to extension method with more "speaking" name like FindAncestor:</p> <pre><code>public static class UIElementExtensions { public static T FindAncestor&lt;T&gt;(this UIElement control) where T: UIElement { UIElement p = VisualTreeHelper.GetParent(control) as UIElement; if (p != null) { if (p is T) return p as T; else return p.FindAncestor&lt;T&gt;(); } return null; } } </code></pre> <p>and use it:</p> <pre><code>DataGrid p = dataGridColumn.FindAncestor&lt; DataGrid &gt;(); </code></pre> <p>If you need to get your DataGrid from XAML try to use binding from <a href="http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/" rel="nofollow">this article</a>.</p> <p>Good luck.</p> <p><strong>UPDATE</strong>:</p> <p>I understand what is the matter. The next answer won't be so easy, but it's silverlight :) So, why you cann't find DataGrid from DataGridColumn using VisualTreeHelper? Because, DataGridColumn doesn't exist in Visual Tree. DataGridColumn inherits from DependencyObject, not UIElement. Forget about VisualTree, and new <strong>idea</strong> will be like this: we <strong>add addition attached property to DataGridColumn - named Owner, and bound DataGrid to this property</strong>. But, <strong>DataGridColumn is DependencyObject</strong> and <strong>any bindings by ElementName don't work in silverlight 4</strong>. We can bind only to StaticResource. So, do it. 1) Owner attached property for DataGridColumn:</p> <pre><code>public class DataGridHelper { public static readonly DependencyProperty OwnerProperty = DependencyProperty.RegisterAttached( "Owner", typeof(DataGrid), typeof(DataGridHelper), null)); public static void SetOwner(DependencyObject obj, DataGrid tabStop) { obj.SetValue(OwnerProperty, tabStop); } public static DataGrid GetOwner(DependencyObject obj) { return (DataGrid)obj.GetValue(OwnerProperty); } } </code></pre> <p>2) DataGrid Xaml (for instance):</p> <pre><code>&lt;Controls:DataGrid x:Name="dg" ItemsSource="{Binding}"&gt; &lt;Controls:DataGrid.Columns&gt; &lt;Controls:DataGridTextColumn h:DataGridHelper.Owner="[BINDING]"/&gt; &lt;/Controls:DataGrid.Columns&gt; &lt;/Controls:DataGrid&gt; </code></pre> <p>3) DataGrid Container - keeper of DataGrid instance in StaticResource:</p> <pre><code>public class DataGridContainer : DependencyObject { public static readonly DependencyProperty ItemProperty = DependencyProperty.Register( "Item", typeof(DataGrid), typeof(DataGridContainer), null ); public DataGrid Item { get { return (DataGrid)GetValue(ItemProperty); } set { SetValue(ItemProperty, value); } } } </code></pre> <p>4) Add to Resources of your view instance of DataGridContainer and bind DataGrid instance to Item property:</p> <pre><code>&lt;c:DataGridContainer x:Key="ownerContainer" Item="{Binding ElementName=dg}"/&gt; </code></pre> <p>Here, binding by ElementName will be worked.</p> <p>5) Last step, we bind our DataGrid to attached property Owner (see p.2 and add next code to [BINDING] section):</p> <pre><code>{Binding Source={StaticResource ownerContainer}, Path=Item} </code></pre> <p>That's all. In code, if we have reference to DataGridColumn we can get owned DataGrid:</p> <pre><code>DataGrid owner = (DataGrid)dataGridColumn.GetValue(DataGridHelper.OwnerProperty);** </code></pre> <p>Hope this principle will help you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload