WPF MVVM列表框中的多项选择

WPF MVVM列表框中的多项选择,wpf,mvvm,listbox,caliburn.micro,multipleselection,Wpf,Mvvm,Listbox,Caliburn.micro,Multipleselection,我有一个包含文件名的列表框。现在我需要从这个列表框中获取所选项目的数组。我在这里找到了一些答案,但没有一个对我有用。我使用的是Caliburn微框架 以下是我的看法: <Window x:Class="ProgramsAndUpdatesDesktop.Views.DeleteHistoryView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schem

我有一个包含文件名的列表框。现在我需要从这个列表框中获取所选项目的数组。我在这里找到了一些答案,但没有一个对我有用。我使用的是Caliburn微框架

以下是我的看法:

<Window x:Class="ProgramsAndUpdatesDesktop.Views.DeleteHistoryView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ProgramsAndUpdatesDesktop.Views"
    mc:Ignorable="d"
    ResizeMode="CanResizeWithGrip"
    MaxWidth="300"
    MinWidth="300"
    MinHeight="500"
    Title="DeleteHistoryView" Height="500" Width="300">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="30" />
    </Grid.RowDefinitions>
    <StackPanel Grid.Column="0" Grid.Row="0">
        <ListBox x:Name="DeleteHistoryListBox" SelectedItem="{Binding Path=DeleteHistorySelectedItem}" 
                 ItemsSource="{Binding DeleteHistoryListBox, NotifyOnSourceUpdated=True}" 
             SelectionMode="Multiple">
        </ListBox>
    </StackPanel>
    <StackPanel Grid.Column="0" Grid.Row="1">
        <Button x:Name="DeleteHistoryButtonAction">Delete</Button>
    </StackPanel>
</Grid>

删除

这是我的ViewModel:

class DeleteHistoryViewModel : Screen
{
    string historyFolderPath = Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSettings["HistoryFolderPath"]);

    private ObservableCollection<string> deleteHistoryListBox = new ObservableCollection<string>();
    public ObservableCollection<string> DeleteHistoryListBox
    {
        get { return deleteHistoryListBox; }
        set { deleteHistoryListBox = value; NotifyOfPropertyChange(() => DeleteHistoryListBox); }
    }

    private List<string> deleteHistorySelectedItem = new List<string>();
    public List<string> DeleteHistorySelectedItem
    {
        get { return deleteHistorySelectedItem; }
        set { deleteHistorySelectedItem = value; }
    }

    public DeleteHistoryViewModel()
    {
        base.DisplayName = "Delete History";
    }

    protected override void OnInitialize()
    {
        FillListBox();
    }

    private void FillListBox()
    {
        string[] directory = Directory.GetFiles($"{historyFolderPath}\\", "*.json");

        foreach (var item in directory)
        {
            string fileName = System.IO.Path.GetFileName(item).ToString();
            if (!DeleteHistoryListBox.Contains(fileName))
            {
                DeleteHistoryListBox.Add(fileName);
            }
        }
    }

    #region ACTIONS REGION

    // DELETE HISTORY ACTION
    public void DeleteHistoryButtonAction()
    {
        foreach (var item in DeleteHistorySelectedItem)
        {
            MessageBox.Show(item);
        }
    }

    #endregion

}
class DeleteHistoryViewModel:屏幕
{
字符串historyFolderPath=Environment.ExpandEnvironmentVariables(ConfigurationManager.AppSettings[“historyFolderPath”]);
私有ObservableCollection deleteHistoryListBox=新ObservableCollection();
公共可观察收集DeleteHistoryListBox
{
获取{return deleteHistoryListBox;}
设置{deleteHistoryListBox=value;NotifyOfPropertyChange(()=>deleteHistoryListBox);}
}
私有列表deleteHistorySelectedItem=新列表();
公共列表删除历史选择编辑项
{
获取{return deleteHistorySelectedItem;}
设置{deleteHistorySelectedItem=value;}
}
public DeleteHistoryViewModel()
{
base.DisplayName=“删除历史记录”;
}
受保护的重写void OnInitialize()
{
FillListBox();
}
私有void FillListBox()
{
string[]directory=directory.GetFiles($“{historyFolderPath}\\”,“*.json”);
foreach(目录中的var项)
{
string fileName=System.IO.Path.GetFileName(item.ToString();
如果(!DeleteHistoryListBox.Contains(文件名))
{
DeleteHistoryListBox.Add(文件名);
}
}
}
#区域行动区域
//删除历史记录操作
public void DeleteHistoryButtonAction()
{
foreach(DeleteHistorySelectedItem中的变量项)
{
MessageBox.Show(项目);
}
}
#端区
}

您可以将此代码用于MVVM模式

XAML


我面临的问题是,当ListBox中的第二项被选择为“多重选择”时,DeleteHistorySelectedItem不会被调用,我如何确保每次用户在ListBox中进行选择时都会引发此事件?
<ListBox x:Name="DeleteHistoryListBoxItem" SelectedItem="{Binding Path=DeleteHistorySelectedItem,UpdateSourceTrigger=PropertyChanged}" 
         ItemsSource="{Binding DeleteHistoryListBox, NotifyOnSourceUpdated=True}" SelectionMode="Multiple">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Item}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
private ObservableCollection<HistoryItems> deleteHistoryListBox = new ObservableCollection<HistoryItems>();
public ObservableCollection<HistoryItems> DeleteHistoryListBox
{
    get
    {
        return deleteHistoryListBox;
    }
    set
    {
        deleteHistoryListBox = value;
        this.RaisePropertyChanged("DeleteHistoryListBox");
    }
}

private HistoryItems deleteHistorySelectedItem;
public HistoryItems DeleteHistorySelectedItem
{
    get
    {
        return deleteHistorySelectedItem;
    }
    set
    {
        var selectedItems = DeleteHistoryListBox.Where(x => x.IsSelected).Count();
        this.RaisePropertyChanged("DeleteHistorySelectedItem");
    }
}
public class HistoryItems : INotifyPropertyChanged
{
    private string item;

    public string Item
    {
        get { return item; }
        set
        {
            item = value;
            this.RaisePropertyChanged("Item");
        }
    }

    private bool isSelected;

    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            isSelected = value;
            this.RaisePropertyChanged("IsSelected");
        }
    }
}