2011-07-14 96 views
0

我對DatagridComboboxColumn有一個類似的問題,但它告訴我如何使用.itemsource綁定到數據網格之外的數組。我在嘗試綁定到數據網格在運行時綁定的集合時遇到問題。WPF VB.NET 4.0 DatagridComboBoxColumn運行時綁定

我已經包含了一個工作測試程序,用於處理這個問題。

Class MainWindow 

Dim ServerInfoArray As List(Of ServerInfo) = New List(Of ServerInfo) 

Private Sub GetInfo(ByVal list As List(Of String)) 
    For Each server As String In list 

     Dim tempip As ComboBoxItem = New ComboBoxItem 
     Dim tempip2 As ComboBoxItem = New ComboBoxItem 
     Dim sinfo As ServerInfo = New ServerInfo 

     tempip.Content = "192.129.123.23" 
     tempip2.Content = "23.213.223.21" 

     sinfo.IPArray.Items.Add(tempip) 
     sinfo.IPArray.Items.Add(tempip2) 
     sinfo.ServerName = server 

     ServerInfoArray.Add(sinfo) 
     DataGrid1.Items.Refresh() 
    Next 
End Sub 

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click 

    Dim serverlist As List(Of String) = New List(Of String) 
    serverlist.Add("Test") 
    serverlist.Add("Random") 
    serverlist.Add("Local") 
    GetInfo(serverlist) 
End Sub 

Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded 
    Dim Col_Serial As DataGridTextColumn = New DataGridTextColumn() 
    Col_Serial.Binding = New Binding("Servername") 
    Col_Serial.Header = "Servername" 
    Col_Serial.Width = 40 

    Dim Col_IPArray = New DataGridComboBoxColumn() 
    Col_IPArray.Header = "IP Addresses" 
    Col_IPArray.IsReadOnly = True 
    'Col_IPArray.ItemsSource = serverInfoArray ' Don't know how to do this. 
    Col_IPArray.SelectedValuePath = "IPArray" 
    Col_IPArray.DisplayMemberPath = "IPArray" 

    DataGrid1.Columns.Add(Col_Serial) 
    DataGrid1.Columns.Add(Col_IPArray) 
    DataGrid1.ItemsSource = ServerInfoArray 
End Sub 
End Class 

Class ServerInfo 

Dim _Servername As String 
Dim _IPArray As ComboBox 
Public Property Servername() As String 
    Get 
     Return _Servername 
    End Get 
    Set(ByVal value As String) 
     _Servername = value 
    End Set 
End Property 

Public Property IPArray As ComboBox 
    Get 
     Return _IPArray 
    End Get 
    Set(ByVal value As ComboBox) 
     _IPArray = value 
    End Set 
End Property 

Public Sub New() 
    _Servername = Nothing 
    _IPArray = New ComboBox 
End Sub 
End Class 

我可以讓所有的字符串和布爾值綁定。

我不知道如何將此DataGridComboBoxColumn綁定到屬性上附加的列表。我無法使用XAML,因爲我需要在運行時執行此操作。

回答

2
Dim Col_Serial As DataGridComboColumn = New DataGridComboColumn() 
    Col_Serial.ItemSource = GetData(); 
    Col_Serial.SelectedValuePath = "ID_value"; 
    Col_Serial.DisplayMemberPath = "displya_col"; 
    Col_Serial.Header = "Disk4" 
    Col_Serial.Width = 40 
Col_Serial.IsEnabled= false; 
dg.Columns.Add(Col_serial); 
+0

你能舉個快速的例子嗎?這個組合框也應該是隻讀的。我不需要任何用戶輸入。所以你的意思,而不是讓IPArray列表(字符串)它應該是組合框?我以爲我曾嘗試過,但我會再試一次。 – Jasin

+0

好吧我認爲我越來越接近了,我必須從外部綁定itemsource?因爲datagrid綁定到同一個類數組。我仍然無法獲取任何數據。我用我正在嘗試的內容更新了我的代碼。 – Jasin

+0

是的。在Windows窗體中,我已經迭代組合框的每個單元格,並將typecase顯式地作爲組合框並將數據源應用於每個單元格。 –

相關問題