2016-10-28 53 views
1

我有4個包含複選框項目的列表框項目。我試圖將它們從同一個數據集中綁定,但似乎我做得不對。當我用這種方式綁定它們時,只有第四個列表框項目顯示項目名稱,其餘的只顯示空白複選框。WPF - 如何從同一個數據集中綁定多個列表框項目

另外我想知道如果我用這種方式綁定複選框,我以後如何從數據庫訪問選定的ID值(因爲我只設置了內容,而我沒有設置selectedValuePath或類似的東西)?

這裏是我下面的代碼:

XML

<ListBox x:Name="listBox1" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="52" Margin="141,264,0,0" VerticalAlignment="Top" Width="307" SelectionMode="Multiple"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <CheckBox x:Name="checkBox1" Content="{Binding NacinGrejanja}"/> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     <Label Content="Tip Objekta:" HorizontalAlignment="Left" Margin="30,323,0,0" VerticalAlignment="Top"/> 
     <ListBox x:Name="listBox2" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="26" Margin="141,323,0,0" VerticalAlignment="Top" Width="307"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <CheckBox x:Name="checkBox1" Content="{Binding TipObjekta}"/> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     <Label Content="Opis Objekta:" HorizontalAlignment="Left" Margin="31,366,0,0" VerticalAlignment="Top"/> 
     <ListBox x:Name="listBox3" ItemsSource ="{Binding}" HorizontalAlignment="Left" Height="52" Margin="142,366,0,0" VerticalAlignment="Top" Width="307"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <CheckBox x:Name="checkBox3" Content="{Binding OpisObjekta}"/> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     <Label Content="Dodatno:" HorizontalAlignment="Left" Margin="31,437,0,0" VerticalAlignment="Top"/> 
     <ListBox x:Name="listBox4" ItemsSource ="{Binding}" HorizontalAlignment="Left" Height="52" Margin="142,437,0,0" VerticalAlignment="Top" Width="307"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <CheckBox x:Name="checkBox4" Content="{Binding Dodatno}" /> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

C#

private void Window_Loaded_1(object sender, RoutedEventArgs e) 
    { 
     Dataset1 ds= new Dataset1(); 
     GrejanjeTableAdapter gta = new GrejanjeTableAdapter(); 
     TipObjektaTableAdapter tta = new TipObjektaTableAdapter(); 
     OpisObjektaTableAdapter ota = new OpisObjektaTableAdapter(); 
     DodatnoTableAdapter dta = new DodatnoTableAdapter(); 
     gta.Fill(ds.Grejanje); 
     tta.Fill(ds.TipObjekta); 
     ota.Fill(ds.OpisObjekta); 
     dta.Fill(ds.Dodatno); 
     this.DataContext = ds.Grejanje; 
     this.DataContext = ds.TipObjekta; 
     this.DataContext = ds.OpisObjekta; 
     this.DataContext = ds.Dodatno; 
    } 

回答

1

取而代之的是:

this.DataContext = ds.Grejanje; 
    this.DataContext = ds.TipObjekta; 
    this.DataContext = ds.OpisObjekta; 
    this.DataContext = ds.Dodatno; 

你應該試試這個:

listbox1.ItemsSource = ds.Grejanje; 
    listbox2.ItemsSource = ds.TipObjekta; 
    listbox3.ItemsSource = ds.OpisObjekta; 
    listbox4.ItemsSource = ds.Dodatno; 

而且您不需要ListBox項目上的ItemsSource="{Binding}"

要獲得所選的項目,你需要的CheckBox項目的IsChecked屬性綁定到一個IsChecked布爾屬性,像這樣:

<CheckBox x:Name="checkBox1" IsChecked={Binding IsSelected} Content="{Binding NacinGrejanja}"/> 

如果NacinGrejanja不包含IsChecked財產,那麼你應該用一個包含它的類來包裝它。

NacinGrejanja的ID應該包含在實例中,因此您將不會有任何問題獲取所選項目的ID。如果不是,那麼再包裝一個類。

+0

它的工作,但它沒有顯示我checkBox項目的文本沒有我把ListSource中的ItemsSource =「{Binding}」。另外,我不確定我是否理解NacinGrejanja ID的含義? – Adicili

+0

關於ID的事情,我回答了你的問題「我如何以後從數據庫中訪問ID值」 –

+0

嗨,我仍然在努力檢查是否有任何複選框被選中,如果它是從數據庫中獲取值。我可以請你看看我已經打開的這個新話題:http://stackoverflow.com/q/40452039/5721684。謝謝 – Adicili

相關問題