如何获取选定的列表框项以更新另一个列表框?

时间:2016-01-15 20:47:03

标签: c# wpf mvvm

我对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类中的方法或我应该做什么。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

根据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>

答案 1 :(得分:0)

我建议将其他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;
    }
}

您需要将转换器放在App或Window的Resources部分,以便在ListBox中使用它:

<this:IsCheckedConverter x:Key="MyIsCheckedConverter" />

然后你对其他ListBox的ItemsSource的绑定看起来像这样:

<ListBox ItemsSource="{Binding Target, Converter={StaticResource MyIsCheckedConverter}}>

答案 2 :(得分:0)

我建议使用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);
                }
            }));
        }
    }