【问题标题】:How to get Selected items in WPF CheckBox ListBox如何在 WPF CheckBox ListBox 中获取选定项目
【发布时间】:2011-05-01 15:53:45
【问题描述】:

在列表框项目中使用复选框,如何从列表中获取选中的复选框

<ListBox ItemsSource="{Binding NameList}"  HorizontalAlignment="Left" Margin="16,68,0,12" Name="listBox1" Width="156" IsEnabled="True" SelectionMode="Multiple" Focusable="True" IsHitTestVisible="True" IsTextSearchEnabled="False" FontSize="12" Padding="5" SelectionChanged="listBox1_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate> 
                        <StackPanel Orientation="Horizontal">                      
                               <CheckBox Content="{Binding Path=CNames}" />
                        </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我试图循环遍历列表框项中的选定项目,但它在列表框项中引发异常

 private void btnSelected(object sender, RoutedEventArgs e)
    {
        foreach (ListBoxItem item in listBox1.Items)
        {
            if (item.ToString() == "true")
            {
                MessageBox.Show(item.Content.ToString());
            }
        }
    }

【问题讨论】:

  • 什么是异常,从哪一行抛出异常?

标签: c# wpf listbox checkbox


【解决方案1】:

您可以将每个项目的数据上下文从 UI 中移开并创建一个 ObservableCollection 对象

public ObservableCollection<CheckedItem> List { get;set;}

public class CheckedItem : INotifyPropertyChanged
{
  private bool selected;
  private string description;

  public bool Selected 
  { 
     get { return selected; }
     set 
     {
        selected = value;
        OnPropertyChanged("Selected");
     }
  }

  public string Description 
  { 
     get { return description; }
     set
     {
         description = value;
         OnPropertyChanged("Description");
     }
   }

  /* INotifyPropertyChanged implementation */
}

然后在你的 ListBox ItemTemplate 中

<ItemTemplate>
  <DataTemplate>
    <CheckBox IsChecked="{Binding Path=Selected}" 
              Content={Binding Path=Description}" />
  </DataTemplate>
</ItemTemplate>

您选择的项目现在在 ObservableCollection 中可用,而不是在 UI 元素中循环

【讨论】:

  • 这就是我在过去 +1 中实现相同的方式。一个小问题:您可能希望在您的CheckedItem 上实现INotifyPropertyChanged,以防需要双向绑定。
  • 同意 INotifyPropertyChanged。这确实取决于您的要求
【解决方案2】:

让你的模板像这样

<ListBox.ItemTemplate>
   <DataTemplate> 
 ........
   <CheckBox Content="" 
      IsChecked="{Binding IsSelected, Mode=TwoWay,
      RelativeSource={RelativeSource FindAncestor, 
      AncestorType={x:Type ListViewItem}}}"  />
 ..........
    <!-- Use Other UIElements to Show your Data -->

那么上面的绑定将通过两种方式与您的模型 isSelected 和列表视图选择同步,然后在代码中使用 SelectedItems。

For Each s As myPoco In myListView1.SelectedItems
   ' do something here with
Next

【讨论】:

    【解决方案3】:

    我会建议这个代码:

     private void save_Click(object sender, RoutedEventArgs e)
     {
         foreach (CheckBox item in list1.Items)
         {
             if (item.IsChecked)
             {
                 MessageBox.Show(item.Content.ToString());
             }
         }
     }
    

    【讨论】:

      猜你喜欢
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2011-01-04
      • 2013-08-29
      相关资源
      最近更新 更多