2013-02-28 85 views
1

沒有人知道如何做到這一點?如何以編程方式綁定DataGrid.SelectedCells

我知道如何在代碼

DataGrid DGrid = new DataGrid(); 
DGrid.SetBinding(... , myBinding); 
結合

但不SelectedCells因爲非DataGrid.SelectedCellsProperty我發現只有DataGrid.SelectedIndexPropertyDataGrid.SelectedItemProperty

任何可能的幫助感激!

回答

2

如果你有一些其他的依賴屬性,你可以將它綁定到DataGrid.SelectedCells屬性。 因此,例如,讓我們假設你有一些DependencyObject祖先和裏面你有一些SelectedCellsInGrid依賴屬性,在這種情況下,你可以調用這樣的事情:

var binding = new Binding("SelectedCells") { Source = yourDataGrid }; 
this.SetBinding(<your_object_type>.SelectedCellsInGridProperty, binding); 

在情況下,如果你沒有依賴對象,你可以在自定義書面行爲中做到這一點。如果這是你的情況,留下評論 - 如果需要,我會提供一些額外的細節。

+0

哦,我不會寫一個行爲,因爲我看起來像一個混亂的事情,所以如果我明白你的權利,將不得不創建一個'DependencyObject'也不是我所期望的。有沒有其他方法可以做到這一點? – WiiMaxx 2013-02-28 09:00:08

+0

如果您試圖從Window或UserControl中訪問DataGrid控件,則可以在其中聲明DependencyProperty(因爲所有這些都是從DependencyObject派生的)。 – Woodman 2013-02-28 10:12:56

+0

我在我的'ViewModel'中構建了我的'DataGrid',並將它作爲'View'的'Property'提供,現在我將從'ViewModel'與另一個'Property'綁定到'SelectedCells'這就是我想要的: )。當然,我可以「綁定」到「selectedCellsChangedEvent」,但我不喜歡它 – WiiMaxx 2013-02-28 10:56:00

相關問題