2011-11-10 30 views
2

在我的Windows.Resources我有以下列定義:編程方式添加一個DataGrid列使用自定義頭的DataGrid

<DataGridTextColumn x:Key="CustomColumn" x:Shared="False"> 
     <DataGridTextColumn.Header> 
      <StackPanel> 
       <Label Padding="0" Name="labelA"/> 
       <Separator HorizontalAlignment="Stretch"/> 
       <Label Padding="0" Name="labelB"/> 
      </StackPanel> 
     </DataGridTextColumn.Header> 
    </DataGridTextColumn> 

我有一個會從我的ViewModel發射並增加了以下「CustomColumn」事件到我的DataGrid:

  var column = FindResource("CustomColumn") as DataGridTextColumn; 
      var label = FindName("labelA") as Label; 
      label.Content = string.Format("A {0}", i); 
      DataGrid1.Columns.Add(column); 

問題是,我將如何更改CustomColumn頭內的兩個標籤的內容?我上面的代碼失敗,因爲它無法找到「labelA」。 (添加列的作品,但我也需要設置這些標籤)。我的猜測是,我需要通過VisualTree找到它 - 但我想確保我沒有做任何其他錯誤。

感謝您的幫助。

回答

2

我創造了一些Visual Tree helpers,我用所有的時間來找到在Visual樹對象

例如,你可以找到一個名爲「LabelA」這個標籤:

VisualTreeHelpers.FindChild<Label>(column, "LabelA"); 

這裏的FindChild如果方法上面的鏈接不起作用

using System.Windows; 
using System.Windows.Media; 

namespace MyNamespace 
{ 
    public class VisualTreeHelpers 
    { 
     /// <summary> 
     /// Looks for a child control within a parent by name 
     /// </summary> 
     public static T FindChild<T>(DependencyObject parent, string childName) 
     where T : DependencyObject 
     { 
      // Confirm parent and childName are valid. 
      if (parent == null) return null; 

      T foundChild = null; 

      int childrenCount = VisualTreeHelper.GetChildrenCount(parent); 
      for (int i = 0; i < childrenCount; i++) 
      { 
       var child = VisualTreeHelper.GetChild(parent, i); 
       // If the child is not of the request child type child 
       T childType = child as T; 
       if (childType == null) 
       { 
        // recursively drill down the tree 
        foundChild = FindChild<T>(child, childName); 

        // If the child is found, break so we do not overwrite the found child. 
        if (foundChild != null) break; 
       } 
       else if (!string.IsNullOrEmpty(childName)) 
       { 
        var frameworkElement = child as FrameworkElement; 
        // If the child's name is set for search 
        if (frameworkElement != null &amp;&amp; frameworkElement.Name == childName) 
        { 
         // if the child's name is of the request name 
         foundChild = (T)child; 
         break; 
        } 
        else 
        { 
         // recursively drill down the tree 
         foundChild = FindChild<T>(child, childName); 

         // If the child is found, break so we do not overwrite the found child. 
         if (foundChild != null) break; 
        } 
       } 
       else 
       { 
        // child element found. 
        foundChild = (T)child; 
        break; 
       } 
      } 

      return foundChild; 
     } 
    } 
} 
+0

謝謝你瑞秋。我會試試這個,讓你知道它是如何工作的。 - 我只是不確定是不是因爲Visual Tree。對於我的情況,上述解決方案是最好的方法嗎? –

+0

@iimpact只需重新閱讀您的問題,並且它不會將新列動態添加到您的DataGrid(除非您只添加一列)。原因是你正在重新使用你的'DataGridTextColumn'的單個實例。如果您嘗試添加該單一實例的第二個副本,則會有兩列包含完全相同的標題和數據,因爲您引用的是同一列的實例。要動態添加列到DataGrid,您需要在每次需要新列時創建一個新的「DataGridTextColumn」。 – Rachel

+0

好的,非常感謝。我知道如何以編程方式創建文本列,但自定義標題將如何完成? –

相關問題