我有一个 ListView,因为它支持多项选择,所以我有一个按钮,我可以在其中收集所有 SelectedItems 并使用 CommandParameter 传递它们。对此我很陌生,我真的不知道如何使用参数。将列表传递给我的 ViewModel 后如何访问该列表?请看下面的代码:
看法
<ListView x:Name="ListView" ItemsSource="{Binding myModel.myCollection}">
<Button Command="{Binding SelectBtnOnClickCommand}" CommandParameter="{Binding SelectedItems, ElementName=ListView}">
视图模型
public class SiteListViewModel
{
public ICommand AddBtnOnClickCommand { get; }
private ICommand _selectBtnOnClickCommand;
public ICommand SelectBtnOnClickCommand
{
get
{
if (_selectBtnOnClickCommand == null)
_selectBtnOnClickCommand = new RelayCommand(o =>
{
var selectedSites = (o as IList);
if (selectedSites != null)
{
foreach (var model in selectedSites.OfType<SiteUrlsModel>())
{
//
}
}
});
return _selectBtnOnClickCommand;
}
}
private readonly IWindowService _windowService;
public static SiteUrlsModel SiteUrlsModel { get; } = new SiteUrlsModel();
public ObservableCollection<SiteUrlsModel> SelectedSites { get; set; }
private readonly ClientContext _clientContext = new ClientContext();
public SiteListViewModel(IWindowService windowService)
{
_windowService = windowService;
AddBtnOnClickCommand = new RelayCommand(AddBtnOnClick);
//SelectBtnOnClickCommand = new RelayCommand(SelectBtnOnClick);
RefreshSiteListView();
}
public void AddBtnOnClick()
{
_addSiteWindow = new AddSite(this);
_addSiteWindow.Show();
}
public void SelectBtnOnClick(ObservableCollection<SiteUrlsModel> checkedList)
{
foreach (var site in checkedList)
{
site.IsChecked = true;
}
}
慕桂英4014372
相关分类