2011-04-15 37 views
0

我想在代碼中的DataTemplate中獲取一個元素的句柄。我在代碼中創建一系列DataGridTemplateColumns,然後將其分配給網格。 我想能夠從xaml中檢索DataTemplate,找到我的元素並綁定到該特定元素。如何從DataTemplate中檢索元素並綁定到它?

這裏是一個簡短的示例代碼是什麼我想實現:

<DataTemplate x:Key="dataTemplate"> 
    <Grid TextBlock.Foreground="LightGreen" Background="Yellow"> 
     <TextBlock x:Name="txt" /> 
    </Grid> 
</DataTemplate> 
DataGridTemplateColumn col = new DataGridTemplateColumn(); 
col.Header = "Last Name"; 
Binding b = new Binding("LastName"); 
DataTemplate dtemplate = (DataTemplate)FindResource("dataTemplate"); 
TextBlock textBlock = dtemplate.FindName("txt", this); 
textBlock.SetBinding(TextBlock.TextProperty, b); 
col.CellTemplate = dtemplate; 

grid.Columns.Add(col); 

可能進一步解釋這一點: 我試圖在飛行中創建一組DataGridTemplateColumns的並將其應用於Datagrid。因爲我不知道屬性綁定到,直到源被呈現在我面前,我不能創建嵌套本身一個DataTemplate的時候有這種結合已經建立像:

<TextBlock Text={Binding=LastName} ... > 

所以我不得不在運行時創建一組DataGridTemplateColumn,在我的資源中查找DataTemplate,然後嘗試將該列綁定到我的數據源上的屬性(如LastName)。

+2

的DataTemplates只是說明,大公不包含對象的引用。但是您可以更改DataTemplate應用到的對象。 – vorrtex 2011-04-15 16:33:27

+0

+1以前的評論。用你在這裏寫的代碼的類型,你可能會解釋一下大局嗎?當我看到直接處理WPF中的UI元素的代碼不包含在自定義控件中時,它通常會發出紅色標記。 – 2011-04-17 03:22:32

+0

感謝您的迴應 - 我試圖創建一組DataGridTemplateColumns'在飛行',而不是能夠指定(並綁定他們!)他們在XAML。這意味着我必須能夠在指定列後將該列綁定到我的數據源上的dp。 – Marcel 2011-04-17 06:29:27

回答

0

我將通過CellStyle接近這個:

<DataGrid.Resources> 
    <Style x:Key="CellStyle" TargetType="{x:Type DataGridCell}"> 
     <Setter Property="TextBlock.Foreground" Value="LawnGreen"/> 
     <Setter Property="Background" Value="Yellow"/> 
    </Style> 
</DataGrid.Resources> 
DataGridTextColumn col = new DataGridTextColumn(); 
col.Binding = new Binding("Name"); 
col.CellStyle = dataGrid.Resources["CellStyle"] as Style; 
dataGrid.Columns.Add(col); 

也有辦法通過的DataTemplates做到這一點,但它似乎沒有必要在這種情況下(除非你的問題比較複雜) 。

0

我對這個問題的解決辦法是這樣的:

GridView viewLayout = new GridView(); 

     for (int i = 0; i < Apprefs.tables[0].Headers.Count(); i++) 
     { 
      GridViewColumn gvc = new GridViewColumn(); 
      string colName = Apprefs.tables[0].Headers[i]; 
      gvc.Header = colName; 
      gvc.Width = 80; 
      gvc.CellTemplate = SetTemplete(i); ; 
      viewLayout.Columns.Add(gvc); 
     } 

     listview1.View = viewLayout; 

     //set binding 
     listview1.ItemsSource = Apprefs.tables[0].Rows; 

然後:

/// <summary> 
    /// Create DataTemplate 
    /// </summary> 
    /// <param name="i"></param> 
    /// <returns></returns> 
    private DataTemplate SetTemplete(int i) 
    { 
     DataTemplate template = new DataTemplate(); 

     //set stack panel 
     FrameworkElementFactory sp = new FrameworkElementFactory(typeof(StackPanel)); 
     sp.Name = "spBind"; 
     sp.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); 

     //set textblock 
     FrameworkElementFactory tb = new FrameworkElementFactory(typeof(TextBlock)); 
     sp.Name = "tbBind"; 
     Binding b = new Binding(); 
     string ito = "[" + i.ToString() + "]"; // bind by index 
     b.Path = new PropertyPath(ito); 
     tb.SetBinding(TextBlock.TextProperty, b); 
     sp.AppendChild(tb); 

     //set the visual tree of the data template 
     template.VisualTree = sp; 

     return template; 
    } 
相關問題