gpt4 book ai didi

wpf - MVVM WPF 中 ListView 复选框的选定项

转载 作者:行者123 更新时间:2023-12-04 15:49:07 26 4
gpt4 key购买 nike

我想在 View 模型中传递 ListView 复选框的选定项目,稍后我将使用进一步的过程存储在数据库中。

FormWeek.xaml 中的代码为

<Window.DataContext>
<Binding Source="{StaticResource Locator}" Path="TaskExecDefModel"></Binding>
</Window.DataContext>
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="ItemDataTemplate">
<CheckBox
x:Name="checkbox"
Content="{Binding}" Command="{Binding CheckBoxCommand}" CommandParameter="{Binding ElementName=checkedListView, Path=SelectedItems}"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />
</DataTemplate>
</ResourceDictionary>

<StackPanel Grid.Column="1" Grid.Row="3" Margin="5">
<CheckBox x:Name="selectAll" Content="Select all" Click="OnSelectAllChanged"/>

<ListView x:Name="checkedListView" SelectionMode="Multiple" ItemsSource="{Binding CollectionOfDays}" DataContext="{Binding}"
ItemTemplate="{StaticResource ItemDataTemplate}" SelectedValue="WorkingDay"
CheckBox.Unchecked="OnUncheckItem" SelectionChanged="SelectDays" SelectedItem="{Binding SelectedItems}">
</ListView>

</StackPanel>

FormWeek.xaml.cs 中的代码

 private void SelectDays(object sender, SelectionChangedEventArgs e)
{
(this.DataContext as TaskExecDefinitionViewModel).OnCheckBoxCommand(checkedListView.SelectedItems,true);

}

我的 View 模型TaskWeek.cs如下

//声明

private RelayCommand<object> _checkBoxCommand;

public ObservableCollection<string> CollectionOfDays { get; set; }
public ObservableCollection<string> SelectedItems { get; set; }

public RelayCommand<object> CheckBoxCommand
{
get
{
if (_checkBoxCommand == null)
{
_checkBoxCommand = new RelayCommand<object>((args) => OnCheckBoxCommand(args,true));
// _checkBoxCommand = new RelayCommand(() => OnCheckBoxCommand(object args));
}
return _checkBoxCommand;
}
}

//构造函数

CollectionOfDays = new ObservableCollection<string>();

//方法

private void GetWeekDays()
{
try
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{

CollectionOfDays.Add("Saturday");
CollectionOfDays.Add("Sunday");
CollectionOfDays.Add("Monday");
CollectionOfDays.Add("Tuesday");
CollectionOfDays.Add("Wednesday");
CollectionOfDays.Add("Thursday");
CollectionOfDays.Add("Friday");
}));
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message, "TaskWeek:GetWeekDays");
}
}

public void OnCheckBoxCommand(object obj, bool _direction)
{
try
{

if (SelectedItems == null)
SelectedItems = new ObservableCollection<string>();

if (obj != null)
{
SelectedItems.Clear();
StringBuilder items = new StringBuilder();


if (_direction)
{
foreach (string item in CollectionOfDays)
{

items.AppendFormat(item + ",");
}
}

MessageBox.Show(items.ToString());
}
else
SelectedItems.Clear();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "TaskDefinition:OnCheckBoxCommand");
}
}

下面是按钮点击commond保存数据。

<Button Grid.Column="2" Grid.Row="2" Content="Save" HorizontalAlignment="Center" VerticalAlignment="Bottom" 
Command="{Binding InsertExecDefCommand}" Margin="5" >

现在我的要求是通过命令对象将选定的 ListView 项传递给 View 模型。我通过 SelectionChanged 事件使用 FormWeek.xam.cs 中的以下代码完成了此操作

private void OnSelectedItems(object sender, RoutedEventArgs e)
{
StringBuilder items = new StringBuilder();
foreach (string item in checkedListView.SelectedItems)
{
items.AppendFormat(item + ",");
}
string AllDays= items.ToString();
}

但是请告诉我如何通过MVVM实现这个逻辑。如何在我的 View 模型 TaskWeek.cs 中获取选定项

在研发和谷歌之后,我通过 RelayCommand 完成了更改。在 OnCheckBoxCommand 方法中,foreach 语句是错误的,它一直在传递。我只想传递选定的 ListView 项。请告诉我 OnCheckBoxCommand 方法有什么问题。

最佳答案


这是我的发现;
在 FormWeek.xaml 的代码后面使用此代码:


private void SelectDays(object sender, SelectionChangedEventArgs e)
{
(this.DataContext as TaskExecDefinitionViewModel).OnCheckBoxCommand(checkedListView.SelectedItems,true);
}

And in 'OnCheckBoxCommand': -

 public void OnCheckBoxCommand(object obj, bool _direction)
{
try
{
if (SelectedItems == null) SelectedItems = new ObservableCollection<string>();

if (obj != null)
{
SelectedItems.Clear();
var _list = ((IList)obj).Cast<string>().ToList();
if (_direction)
{
_list.ForEach(item => SelectedItems.Add(item));
}
}
else
SelectedItems.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "TaskDefinition:OnCheckBoxCommand");
}
}

祝你有美好的一天,伙计......继续前进。

关于wpf - MVVM WPF 中 ListView 复选框的选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15051763/

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