2017-04-17 68 views
0

我正在試圖製作帶有複選框的ListBox。如何訪問ListBox項目中的複選框

在XAML我:

<ListBox ItemsSource="{StaticResource ResourceKey=lstMaterialesCL}" SelectionMode="Multiple" Name="lstMaterial" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <CheckBox Name="chkMaterial" Content="{Binding DescCompuesta}"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

和我的列表框看起來:

enter image description here

這是確定的,但看它,當我檢查 「Municipales」 ListBox中的項目沒有被選中,並且當我在列表框「Industriales」中選擇時,它不被檢查

如果我檢查選擇到列表框中的項目,它不會重合w第i個經過

foreach (var item in lstMaterial.SelectedItems) 
{ 
    MessageBox.Show(((MaterialesCL)item).DescCompuesta); 
} 

的項目它顯示我「Oficiales」,「工業家」和「Destrucciones」,但用戶是希望選擇「Municipales」和「Destrucciones」 我怎樣才能使一致與選擇的列表框項目CheckBox檢查CheckBox是否是強制性的?

+0

http://stackoverflow.com/questions/21193242/wpf-checkedlistbox-how-to-get-selected-item –

+0

http://stackoverflow.com/questions/21193242/wpf-checkedlistbox-如何獲得選擇項目 –

+0

檢查這些帖子。希望你會得到一個。 –

回答

1

XAML:
DescCompuestaListCheckListGeneric

<ListBox ItemsSource="{Binding DescCompuestaList}" > 
    <ListBox.ItemTemplate> 
     <HierarchicalDataTemplate> 
      <CheckBox Content="{Binding DescCompuesta}" IsChecked="{Binding IsChecked}"/> 
     </HierarchicalDataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

這裏您CheckListGeneric類列表

public class CheckListGeneric: ViewModelBase 
{ 
    #region ..:: Fields ::.. 

    private bool _isChecked; 

    #endregion 

    #region ..:: Properties ::.. 

    public long Id { get; set; } 
    public string DescCompuesta{ get; set; } 

    public bool IsChecked 
    { 
     get { return _isChecked; } 
     set { _isChecked = value; OnPropertyChanged("IsChecked"); } 
    } 

    #endregion 
} 

你可以得到所有使用簡單的查詢

var selectedItems = DescCompuestaList.Where(x => x.IsChecked)

選擇0

簡單如生活。

+0

thinks這麼多@CelsoLívero我測試你的建議 – Cyndy

1

如何約束CheckBoxIsChecked財產到ListBoxItemIsSelected財產。 喜歡的東西:IsChecked={Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}

在您的例子:

<ListBox ItemsSource="{StaticResource ResourceKey=lstMaterialesCL}" SelectionMode="Multiple" Name="lstMaterial" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <CheckBox Name="chkMaterial" Content="{Binding DescCompuesta}" IsChecked={Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

thnks這麼多@Bubba我測試你的建議 – Cyndy

+0

如果它可以幫助你,不要忘記加大投票並接受答案。 – Bubba

1

對我來說,它總是使用混合製作模板最簡單的方法。 在混合中打開項目並製作一個列表框,然後選擇該列表框並在畫面中添加模板。 enter image description here

只是爲了顯示目的,我添加了簡單的checkBox和TextBlock,並且可以讓它像你一樣。

在視圖模型我做了簡單的觀察集合只是爲了顯示的目的和綁定的ItemsSource到用戶:

public class TestVM 
    { 
     public ObservableCollection<User> Users { get; set; } 

     public TestVM() 
     { 
      Users = new ObservableCollection<User> 
      { 
       new User{ IsChecked=true, Name="User1" }, 
       new User{ IsChecked=false, Name="User2" }, 
       new User{ IsChecked=true, Name="User3" }, 
       new User{ IsChecked=false, Name="User3" }, 
      }; 
     } 
    } 

    public class User 
    { 
     public bool IsChecked { get; set; } 
     public string Name { get; set; } 
    } 

這種方式可以讓你喜歡的任何模板。

0

哇,我測試了所有你的建議,非常感謝你的所有,所有的測試後,該解決方案如何,我需要它或多或少是這樣的:

在XAML:

<StackPanel Orientation="Horizontal"> 
    <ListBox ItemsSource="{Binding MaterialesVM}" SelectionMode="Multiple" 
Name="ListBoxMateriales" Width="300" Height="200" 
HorizontalAlignment="Left" VerticalAlignment="Top"> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="IsSelected" Value="{Binding IsChecked, Mode=TwoWay}" /> 
      </Style> 
     </ListBox.ItemContainerStyle> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" 
Content="{Binding DescCompuesta}"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    <Button Name="btnPrueba" Style="{StaticResource BotonContent}" 
Content="Selected" Width="80" Height="30" HorizontalAlignment="Left" 
VerticalAlignment="Top" 
      Margin="10,0,0,0" Click="btnPrueba_Click"/> 
    <Button Name="btnLimpia" Style="{StaticResource BotonRechazar}" 
Width="30" Height="30" Click="btnLimpia_Click" HorizontalAlignment="Left" 
VerticalAlignment="Top" 
      Margin="5,0,0,0" ToolTipService.ToolTip="Limpia Todos"/> 
    <Button Name="btnMarca" Style="{StaticResource BotonAceptar}" 
Width="30" Height="30" Click="btnMarca_Click" HorizontalAlignment="Left" 
VerticalAlignment="Top" 
      Margin="5,0,0,0" ToolTipService.ToolTip="Selecciona Todos"/> 
</StackPanel> 

在這條路上,我不就在那裏點擊關心,該項目將被選中或取消選中(複選框上的列表項)

而且看選擇項:

// To show the selected items 
private void btnPrueba_Click(object sender, RoutedEventArgs e) 
{ 
    var selectedItems = MaterialesVM.Where(x => x.IsChecked); 
    foreach (var item in selectedItems) 
    { 
     MessageBox.Show(((MaterialesCL)item).DescCompuesta); 
    } 
} 

要選擇所有項目:

// To select all items 
private void btnMarca_Click(object sender, RoutedEventArgs e) 
{ 
    var Items = ListBoxMateriales.Items; 
    foreach (MaterialesCL item in Items) 
    { 
     item.IsChecked = true; 
    } 
} 

要取消選擇所有項目:

// To un-select all items 
private void btnLimpia_Click(object sender, RoutedEventArgs e) 
{ 
    var Items = ListBoxMateriales.Items; 
    foreach (MaterialesCL item in Items) 
    { 
     item.IsChecked = false; 
    } 
} 

和我的課是:

MaterialesCL.cs

public class MaterialesCL : INotifyPropertyChanged 
{ 
    public int Material { get; set; } 
    public string Descripcion { get; set; } 
    public string DescCompuesta { get; set; } 
    private bool _ischecked; 
    public bool IsChecked 
    { 
     get { return _ischecked; } 
     set 
     { 
      _ischecked = value; 
      OnPropertyChanged("IsChecked"); 
     } 
    } 

    #region PropertyChanged 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
    #endregion 
} 

LstMaterialesCL。 cs

public class LstMaterialesCL : ObservableCollection<MaterialesCL>, INotifyPropertyChanged 
{ 
    public LstMaterialesCL() 
    { 
     BasculaEntities _context = new BasculaEntities(); 
     var Query = (from m in _context.Materiales 
         select new { m.Material, m.Descripcion} 
         ).OrderBy(m => m.Material).ToList(); 

     foreach (var item in Query) 
     { 
      this.Add(new MaterialesCL { Material = item.Material, 
       Descripcion = item.Descripcion, DescCompuesta = item.Material.ToString("000") + " - " + item.Descripcion, 
      IsChecked=false}); 
     } 
    } 
} 

和我的用戶MaterialesVM是LstMaterialesCL

的實例
LstMaterialesCL MaterialesVM = new LstMaterialesCL(); 

所以我用的CheckBox列表框的測試工作,因爲我需要。

謝謝你我所學到的一切。

enter image description here