2013-03-12 59 views
7

基本上我已經綁定了數據網格,因此它類似於科目的時間表 - 每一行代表一學期的科目,並且該學期內的每個單元格代表一個科目。如何在代碼中訪問DataGridCell的數據對象?

我現在試圖添加拖放功能,以便您可以將其他主題拖動到網格上,這將更新基礎數據結構。

我可以使用一些可視化樹方法來查找用戶拖動新主題的DataGridCell,但我不知道如何訪問單元格綁定到的值(主題)以便用新主題替換空白/佔位符值。有沒有辦法訪問底層的價值,還是我應該重構我創建這個程序的整個方法?

An example of the grid and the subjects to be dragged onto it

+0

你可以粘貼網格結構嗎? – Freelancer 2013-03-12 12:47:14

+0

物理GridView或底層結構? – Miguel 2013-03-12 12:48:10

+0

只是列和實用數據 – Freelancer 2013-03-12 12:49:09

回答

3

爲了得到DataGridCell的數據,你可以用它的DataContext和Column財產。如何做到這一點完全取決於你的行數據,即你在DataGrid的ItemsSource集合中放置了哪些項目。假設你的項目object[]陣列:

// Assuming this is an array of objects, object[],this gets you the 
// row data as you have them in the DataGrid's ItemsSource collection 
var rowData = (object[]) DataGrid.SelectedCells[0].Item; 
// This gets you the single cell object 
var celldata = rowData[DataGrid.SelectedCells[0].Column.DisplayIndex]; 

如果你的行中的數據比較複雜,你需要寫一個翻譯上的行數據項Column屬性和行數據項的具體值的相應的方法。


編輯:

如果你把你的數據到是不是所選單元格的單元格,一種選擇是讓DataGridRow到該DataGridCell屬於使用VisualTreeHelper

var parent = VisualTreeHelper.GetParent(gridCell); 
while(parent != null && parent.GetType() != typeof(DataGridRow)) 
{ 
    parent = VisualTreeHelper.GetParent(parent); 
} 
var dataRow = parent; 

然後你有這一行,並可以按照上述進行。


此外,關於你的問題,你是否應該重新考慮的方法,我會建議使用自定義WPF行爲秒。

行爲提供了一種非常直接的方式來從C#代碼擴展控件的功能,而不是XAML,但保持代碼隱藏清晰和簡單(如果您遵循MVVM,這不僅很好)。行爲的設計方式是可重複使用的,並不受限於您的特定控制。

Here's a good introduction

對於你的特殊情況下,我只能給你做什麼的想法:

寫一個DropBehavior您的TextBlock控件(或任何控制你希望你的DataGridCells,該手柄內下降。其基本思路是根據你的控制OnAttached()方法行動細胞的EVNT註冊。

public class DropBehavior : Behavior<TextBlock> 
{ 
    protected override void OnAttached() 
    { 
     AssociatedObject.MouseUp += AssociatedObject_MouseUp; 
    } 

    private void AssociatedObject_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
     // Handle what happens on mouse up 

     // Check requirements, has data been dragged, etc. 
     // Get underlying data, now simply as the DataContext of the AssociatedObject 
     var cellData = AssociatedObject.DataContext; 

    } 
} 

需要注意的是,解析單細胞的數據來回回行數據和Column屬性變得過時。

然後您將這個行爲的TextBlocks,你你的細胞裏面放,使用DataGrid的CellStyleContentTemplate

<DataGrid> 
    <DataGrid.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Setter Property="ContentTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <TextBlock Text="{Binding}"> 
          <i:Interaction.Behaviors> 
           <yourns:DropBehavior/> 
          </i:Interaction.Behaviors> 
         </TextBlock> 
        </DataTemplate> 
       </Setter.Value> 

      </Setter> 
     </Style> 
    </DataGrid.CellStyle> 
</DataGrid> 

您可以找到Behavior<T>基類中

系統.Windows.Interactivity.dll

我還沒有測試過,但我希望它爲你工作,你會明白...

+0

我會看看WPF行爲的事情,謝謝你的鏈接。然而,對於你的答案的第一部分,我不能做你的任何解決方案。在第一個例子中,你使用DataGrid.SelectedCells [0],它返回一個DataGridCellInfo,我*沒有*。記住我只有DataGridCell,它是在拖放之後從鼠標位置獲得的,我沒有選擇。我可以選擇這個單元格,然後執行此方法,但我寧願不必更改用戶的選擇。 – Miguel 2013-03-13 00:40:48

+0

在第二種方法中,您使用rowData [Column Index]。我可以得到列索引,因爲我有列(它是DataGridCell的一個屬性),但我不知道單元格的當前行以獲得正確的rowData結構。 – Miguel 2013-03-13 00:41:21

+0

@Miguel:我點了我的帖子。我肯定會推薦使用行爲。這真的沒有那麼複雜,而且所有這些尋找父行和鑄造的東西都不是很好的風格和效率。 – Marc 2013-03-13 08:42:36

相關問題