3

我一直在研究清单应用程序,并且正在使用 aListBox来显示数据。我正在使用数组来填充ListBox基于用户按下的按钮。(我不知道这是否是最佳做法,但它现在有效。)用户完成这些步骤后,他可以使用以下命令删除项目:

private void SendSelected()
{
    while (lstToDo.SelectedItems.Count > 0)
    {
        lstToDo.Items.Remove(lstToDo.SelectedItem);
    }
}

问题是今天我学会了如何使用下面的xaml将CheckBoxes添加到我的:ItemTemplate

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <CheckBox Padding="10">                            
                <CheckBox.LayoutTransform>
                    <ScaleTransform ScaleX="1" ScaleY="1" />
                </CheckBox.LayoutTransform>
            </CheckBox>
            <TextBlock Text="{Binding}"/>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

现在删除按钮仍然有效......但是我想对其进行调整,以便它删除选中的项目而不是选定的项目。在winforms中,我曾经这样做过:

while (lstToDo.CheckedItems.Count > 0)
{
    lstToDo.Items.Remove(lstToDo.CheckedItems[0]);
}  

但在 WPF 中,显然这种方法不起作用,我只是不知道为什么。

4

4 回答 4

2

您可以将复选框绑定到 ListBoxItem 的 IsSelected 属性

<DataTemplate>
    <CheckBox Content="{Binding .}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected" />
</DataTemplate>
于 2013-01-16T21:15:29.457 回答
0

对您的数据使用包装器,其中将包括特定于视图的数据,例如 IsChecked 属性。将属性绑定到CheckBox,然后你就可以找到所有被选中的项目...

简单示例:

public class CheckedItem<T> where T : class
{
    public T Data { get; set; }
    public bool IsChecked { get; set; }
}

所以不是你的列表,MyItem而是创建一个列表CheckedItem<MyItem>

不只是相应地更改绑定:

<ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Padding="10" IsChecked={Binding IsChecked}>                            
                    <CheckBox.LayoutTransform>
                        <ScaleTransform ScaleX="1" ScaleY="1" />
                    </CheckBox.LayoutTransform>
                </CheckBox>
                <TextBlock Text="{Binding Data}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

如果您只想删除已检查的项目,您可以像这样获取它们:

List<CheckedItem<MyItem>> MyItems; 
...
...

foreach (var Item in MyItems)
{
    if (!Item.IsChecked)
    {
         lstToDo.Items.Remove(Item);
    }
}
于 2013-01-16T21:16:20.770 回答
0
 private void RemoveButton_Click(object sender, RoutedEventArgs e)
  {   
   foreach(CheckedItem in lstToDo.SelectedItems)
  {
    (lsttoDo.ItemsSource as List).Remove(CheckedItem);
  }
    lsttoDo.Items.Refresh();
 }

在你的c#.

于 2013-01-16T21:27:27.397 回答
0

您可以只Model为您创建一个带有 Checked 属性的 Items,这样,如果您更改 UI 中的数据,MyList则会更新,如果您MyList在代码中更新项目,UI 将反映更改。

因此,如果您从中删除所有选中的项目,MyList它们将从ListBox

public partial class MainWindow : Window
{
    private ObservableCollection<MyObject> _myItems = new ObservableCollection<MyObject>();

    public MainWindow()
    {
        InitializeComponent();
        MyItems.Add(new MyObject { Name = "Test1" });
        MyItems.Add(new MyObject { Name = "Test2" });
        MyItems.Add(new MyObject { Name = "Test3" });
        MyItems.Add(new MyObject { Name = "Test4" });
    }

    public ObservableCollection<MyObject> MyItems
    {
        get { return _myItems; }
        set { _myItems = value; }
    }

    public void RemoveItems()
    {
        // Remove any items fro MyList that "IsChecked"
    }
}

public class MyObject : INotifyPropertyChanged
{
    private string _name;
    private bool _isChecked;

    public string Name
    {
        get { return _name; }
        set { _name = value; NotifyPropertyChanged("Name"); }
    }

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

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

xml:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="199" Width="206" Name="UI">
    <Grid DataContext="{Binding ElementName=UI}">
        <ListBox ItemsSource="{Binding MyItems}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Padding="10" IsChecked="{Binding IsChecked}">
                            <CheckBox.LayoutTransform>
                                <ScaleTransform ScaleX="1" ScaleY="1" />
                            </CheckBox.LayoutTransform>
                        </CheckBox>
                        <TextBlock Text="{Binding Name}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>
于 2013-01-16T21:28:15.140 回答