2012-06-17 40 views
1

在xaml中,我有一個帶有1行2列的網格的列表框。在第一列中我有一個名稱,在第二列中我有另一個列表框......我有第一個列表框綁定到可觀察的附件項目集合。在Enclosure類中,我有另一個可觀察的Servers集合(另一個類)。我正試圖將此綁定到列表框。當我將項目添加到可觀察集合時,EnclosureID正在正常工作和更新。但是,我不太確定如何讓Slist綁定到我在其他列表框中的列表框。有人有主意嗎?我可以使用另一種方法嗎?將列表框內的列表框綁定到可觀察集合?最佳方法?

public class Enclosure 
    { 
     private string enclosureID; //bound to the first listbox 

     //I want to bind this below to the second listbox 
     //There is another class called Server with various properties 
     public ObservableCollection<Server> Slist = new ObservableCollection<Server>(); 

     public string EnclosureID 
     { 
      get { return enclosureID; } 
      set { enclosureID = value; } 
     } 
    } 

在XAML:

<ListBox x:Name="lb1" ItemsSource="{Binding}" DataContext="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 

     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition/> 
        </Grid.RowDefinitions> 

        <Grid.ColumnDefinitions> 
         <ColumnDefinition/> 
         <ColumnDefinition/> 
        </Grid.ColumnDefinitions> 

        <TextBlock x:Name="txtEnclosure" Text="{Binding Path=EnclosureID}" Background="Aqua" Grid.Column="0" Grid.Row="0" /> 
        <ListBox x:Name="lbserver" ItemsSource="{Binding Slist}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.Column="1" Grid.Row="0"> 
         <ListBox.ItemsPanel> 
          <ItemsPanelTemplate> 
           <WrapPanel/> 
          </ItemsPanelTemplate> 
         </ListBox.ItemsPanel> 

         <ListBox.ItemTemplate> 
          <DataTemplate> 
           <TextBlock x:Name="txtServer" Text="{Binding Path=HostnameID}" Background="Beige"/> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
        </ListBox> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

要填充列表我用這個方法:

  //Query the database for enclosures and populate the Enclosure observable collection in the Settings class 
    public void GetEnclosures() 
    { 
     bool exception = false; 
     Enclosure enclosure = new Enclosure(); 
     Server server = new Server(); 
     server.HostnameID = "HEY"; 

     OleDbCommand GetEnclosuresCommand = new OleDbCommand(Settings.GetEnclosuresQuery, Settings.conn); 

     Settings.conn.Open(); 
     try 
     { 
      Settings.myReader = GetEnclosuresCommand.ExecuteReader();//begin reading 
     } 
     catch 
     { 
      MessageBox.Show("The Enclosure table is currently being used by another application. Please close the table and run this application again."); 
      Settings.conn.Close(); 
      exception = true; 
     } 

     if (!exception) 
     { 
      // while there are enteries to retrieve 
      while (Settings.myReader.Read()) 
      { 
       enclosure.EnclosureID = Settings.myReader.GetString(0); 
       enclosure.Slist.Add(server); 
       Settings.Elist.Add(enclosure); 
       enclosure = new Enclosure(); 
       server = new Server(); 
       server.HostnameID = "HI"; 
      } 

      // Close when done reading. 
      Settings.myReader.Close(); 

      // Close the connection. 
      Settings.conn.Close(); 
     } 
     else 
     { 
      this.NavigationService.Navigate(new Uri("MainPage.xaml", UriKind.Relative)); 
     } 

    } 
+0

我想我有一個想法,什麼錯誤,但你也張貼調用GetEnclosures並設置UI的DataContext的代碼? –

+0

其實我覺得我知道這個問題,把Slist改成屬性,只需要添加一個getter和setter,否則就不能綁定到UI –

回答

0

可能是錯的,但並不像那樣簡單......

<ListBox 
    x:Name="lbserver" 
    ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
    Grid.Column="1" 
    Grid.Row="0" 
    ItemsSource="{Binding Slist}"> 

同樣改變這個

public ObservableCollection<Server> Slist = new ObservableCollection<Server>(); 

public ObservableCollection<Server> Slist { get; set; } 
//... 
Slist = new ObservableCollection<Server>(); 
+0

我試過了,但是好像沒有這樣做= =( – KateMak

+0

Then可能需要查看更多代碼,Slist是如何填充的? –

+0

我正在將示例數據放入其中,以與填充其他集合的方法相同的方式對其進行測試: – KateMak

相關問題