gpt4 book ai didi

c# - 如何使用CommandParameter发送列表

转载 作者:行者123 更新时间:2023-12-03 10:32:15 25 4
gpt4 key购买 nike

我有一个ListView,并且因为它支持多个选择,所以我有一个按钮,我在其中收集所有SelectedItems并使用CommandParameter传递它们。对此我很陌生,我真的不知道该如何使用参数。将列表传递给ViewModel后,如何访问该列表?请参见下面的代码:

看法

<ListView x:Name="ListView"  ItemsSource="{Binding myModel.myCollection}">
<Button Command="{Binding SelectBtnOnClickCommand}" CommandParameter="{Binding SelectedItems, ElementName=ListView}">

View 模型
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;
}
}

public void RefreshSiteListView()
{
var siteUrlsCollection = new ObservableCollection<SiteUrlsModel>(_clientContext.PopulateList());
SiteUrlsModel.SiteUrlsCollection = siteUrlsCollection;
}
}

CommandClass
public class RelayCommand : ICommand
{

private readonly Action<object> _actionWithObject;

public RelayCommand(Action<object> actionWithObject)
{
_actionWithObject = actionWithObject;
}

public bool CanExecute(object parameter)
{
return true;
}

public void Execute(object parameter)
{
if (parameter != null)
_actionWithObject(parameter);
else
_actionWithObject(parameter);
}

public event EventHandler CanExecuteChanged;
}

最佳答案

ListView.SelectedItemsIList:

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;
}
}

关于c# - 如何使用CommandParameter发送列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53765683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com