2011-03-10 33 views
2

如何獲得給定DataGridColumnDataGrid。我在DataGrid的子類中創建了一個附加屬性,該屬性適用於DataGridColumn(我沒有子類別)。它給我的DataGridColumn我申請附加的屬性,但我怎麼得到DataGrid參考本身?我需要兩個。查找DataGrid for DataGridColumn

編輯:

我更感興趣的是什麼,是如何在附加屬性的事件處理程序一個獲得DependencyObject實例,實際上是託管的附加屬性。也就是說,DataGrid而不是該屬性所附的DataGridColumn

<my:MvvmDataGrid x:Name="_dataGrid" ... > 
    <sdk:DataGrid.Columns> 
     <sdk:DataGridTextColumn my:MvvmDataGrid.SingleClickEdit="true" .../> 
    </sdk:DataGrid.Columns> 
</my:MvvmDataGrid> 

我則有被註冊爲PropertyChangedCallbackSingleClickEditProperty附加屬性的元數據的靜態處理OnSingleClickEditPropertyChanged

當該財產所調用(ID EST財產被列上改變),我給出的DataGridTextColumn,當我需要的MvvmDataGrid實例。

回答

2

我認爲你可以使用this

使用此代碼,你可以找到的DataGridColumn的祖先 - 你的視覺樹DataGrid中。此代碼實現靜態函數,但是你可以用更多的「會說話」的名字一樣FindAncestor它更改爲擴展方法:

public static class UIElementExtensions 

{ 

    public static T FindAncestor<T>(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<T>(); 
     } 
     return null; 
    } 
} 

,並使用它:如果你需要從XAML您的DataGrid

DataGrid p = dataGridColumn.FindAncestor<DataGrid>(); 

嘗試使用來自this article的綁定。

祝你好運。

UPDATE

我明白是怎麼一回事。下一個答案不會那麼容易,但它是silverlight :) 那麼,爲什麼你不能使用VisualTreeHelper從DataGridColumn中找到DataGrid?因爲DataGridColumn在Visual Tree中不存在。 DataGridColumn繼承自DependencyObject,而不是UIElement。 忘掉VisualTree,新的想法會是這樣的:我們添加附加屬性到DataGridColumn - 名爲Owner,並綁定DataGrid到這個屬性。 但是,DataGridColumn是DependencyObjectElementName的任何綁定在silverlight 4中都不起作用。 我們只能綁定到StaticResource。所以,做到這一點。 1)所有者附加屬性的DataGridColumn:

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); 
    } 
} 

2)數據網格的Xaml(例如):

<Controls:DataGrid x:Name="dg" ItemsSource="{Binding}"> 
    <Controls:DataGrid.Columns> 
     <Controls:DataGridTextColumn h:DataGridHelper.Owner="[BINDING]"/> 
    </Controls:DataGrid.Columns> 
</Controls:DataGrid> 

3)數據網格容器 - 在StaticResource的DataGrid實例的門將:

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); } 
    } 
} 

4)添加到DataGridContainer的視圖實例的資源並將DataGrid實例綁定到Item屬性:

<c:DataGridContainer x:Key="ownerContainer" Item="{Binding ElementName=dg}"/> 

在這裏,ElementName的綁定將起作用。

5)最後一步,我們結合我們的DataGrid附財產所有者(見第2頁和下面的代碼添加到[裝訂]部分):

{Binding Source={StaticResource ownerContainer}, Path=Item} 

這就是全部。在代碼中,如果我們參考的DataGridColumn我們可以得到所有的數據網格:

DataGrid owner = (DataGrid)dataGridColumn.GetValue(DataGridHelper.OwnerProperty);** 

希望這個原則會幫助你。

+0

我想到了這一點,但當我嘗試獲取DataGridColumn的父項時,「VisualTreeHelper.GetParent」會引發異常。 – Jordan 2011-03-11 15:45:17

+0

您在第2步中的「[綁定]」是什麼意思?您的意思是「{綁定}」?如果是這樣,那不會有幫助。 – Jordan 2011-03-16 13:26:55

+0

請參閱第5步。我希望您應該在此處添加 – 2011-03-16 13:44:33