[英]How to get selected listbox item to update another listbox?
我是wpf和mvvm的新手,我希望这是有道理的...
我有一个CheckBox项目的ListBox。 当我选中或取消选中某个项目时,我想知道如何触发事件或使我能够将所选项目文本添加到其他ListBox的功能。
到目前为止,这是我所做的:
XAML:
<ListBox ItemsSource="{Binding Target}" IsEnabled="{Binding IsControlEnabled}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding TitleName}" IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
主要ViewModel类:
private ObservableCollection<CheckServerItem> _target = new ObservableCollection<CheckServerItem>();
小类来处理复选框事件:
public class CheckServerItem : ViewModelBase
{
private bool _isChecked { get; set; }
private string _Title { get; set; }
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
RaisePropertyChanged("IsChecked");
}
}
public string TitleName
{
get { return _Title; }
set
{
_Title = value;
RaisePropertyChanged("TitleName");
}
}
}
此类检查由小型类正确处理,但是我无法弄清楚该类如何在管理其他ListBox的Main ViewModel类中调用方法,或者我应该怎么做。
谢谢您的帮助!
根据菲利波·维加尼(Filippo Vigani)的回答,如果您仅通过鼠标选中/取消选中复选框,也可以执行以下操作:
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding TitleName}"
IsChecked="{Binding IsChecked}"
Command="{Binding DataContext.SelectionChangedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
CommandParameter="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
我建议将其他ListBox的ItemsSource绑定到相同的ObservableCollection,然后使用Converter来获取所选项目。 这样一来,您就不必再搞乱附加和分离事件处理程序了。 转换器将是:
[ValueConversion(typeof(object), typeof(object))]
public class IsCheckedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ObservableCollection<CheckServerItem> result = new ObservableCollection<CheckServerItem>();
foreach(CheckServerItem item in (value as ObservableCollection<CheckServerItem>))
{
if (item.IsChecked)
{
result.Add(item);
}
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
您需要将转换器放在应用程序或窗口的“资源”部分中,以便可以在ListBox中使用它:
<this:IsCheckedConverter x:Key="MyIsCheckedConverter" />
然后,您对其他ListBox的ItemsSource的绑定将如下所示:
<ListBox ItemsSource="{Binding Target, Converter={StaticResource MyIsCheckedConverter}}>
我建议使用ICommand并使用AttachedCommandBehaviour将其绑定到CheckBox的Checked RoutedEvent(可以在nuget上获取它):
Install-Package AttachedCommandBehavior
xaml看起来像这样:
...
xmlns:acb="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
...
<ListBox ItemsSource="{Binding Target}"
IsEnabled="{Binding IsControlEnabled}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding TitleName}"
IsChecked="{Binding IsChecked}">
<acb:CommandBehaviorCollection.Behaviors>
<acb:BehaviorBinding Event="Checked"
Command="{Binding DataContext.SelectionChangedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
CommandParameter="{Binding}" />
<acb:BehaviorBinding Event="Unchecked"
Command="{Binding DataContext.SelectionChangedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
CommandParameter="{Binding}" />
</acb:CommandBehaviorCollection.Behaviors>
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
然后在ViewModel中,您将拥有一个处理check / uncheck事件的命令。 您可以将Prism用于实现ICommand的类(称为DelegateCommand),可以从nuget中获取它:
Install-Package Prism.Core
您的viewmodel中的命令可能类似于:
private DelegateCommand<CheckServerItem> selectionChangedCommand;
public DelegateCommand<CheckServerItem> SelectionChangedCommand
{
get
{
return this.selectionChangedCommand ?? (this.selectionChangedCommand = new DelegateCommand<CheckServerItem>((x) =>
{
if (x.IsChecked)
{
MyOtherList.Add(x);
} else
{
MyOtherList.Remove(x);
}
}));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.