2

我知道这很长,但请多多包涵。

我使用 MVVM 灯光框架在 MVVM 灯光示例中创建了一个与 Laurent Bugnion 的“MyFriends”程序非常相似的 Windows 应用商店程序。

在他的程序中,他使用 gridview 的 SelectedItem 属性来跟踪哪个项目是选定项目。

问题是,我让用户能够在 GridView 上选择多个项目,然后使用 App Bar 上的按钮对它们进行操作。对于这个 SelectedItem 将不起作用。

有谁知道如何使用多选 GridView 进行这项工作?我已经根据 WPF 上的一些文章尝试了 GridViewItem 的 IsSelected 属性,但这似乎不起作用。SelectedTimesheets getter 在调用时总是返回空。这是我到目前为止所拥有的:

MainPage.xaml(绑定到具有子 TimesheetViewModel 可观察集合的 MainViewModel):

<GridView 
    x:Name="itemGridView"
    IsItemClickEnabled="True"
    ItemsSource="{Binding Timesheets}"
    ItemTemplate="{StaticResource TimesheetTemplate}" 
    Margin="10"
    Grid.Column="0"
    SelectionMode="Multiple"
    helpers:ItemClickCommand.Command="{Binding NavigateTimesheetCommand}" RenderTransformOrigin="0.738,0.55"  >
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
        </Style>
    </GridView.ItemContainerStyle>

</GridView>

MainViewModel(从完整代码中删减):

public class MainViewModel : ViewModelBase
{
    private readonly IDataService _dataService;
    private readonly INavigationService _navigationService;

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel(IDataService dataService, INavigationService navigationService)
    {
        _dataService = dataService;
        _navigationService = navigationService;

        Timesheets = new ObservableCollection<TimesheetViewModel>();
        ExecuteRefreshCommand();

    }

    public ObservableCollection<TimesheetViewModel> Timesheets
    {
        get;
        private set;
    }

    public IEnumerable<TimesheetViewModel> SelectedTimesheets
    {
        get { return Timesheets.Where(o => o.IsSelected); }
    }

    private async void ExecuteRefreshCommand()
    {
        var timesheets = await _dataService.GetTimesheets("domain\\user");

        if (timesheets != null)
        {
            Timesheets.Clear();

            foreach (var timesheet in timesheets)
            {
                Timesheets.Add(new TimesheetViewModel(timesheet));
            }

        }
    }
}

时间表视图模型:

public class TimesheetViewModel: ViewModelBase
{

    public bool IsSelected { get; set; }

    public Timesheet Model
    {
        get;
        private set;
    }

    public TimesheetViewModel(Timesheet model)
    {
        Model = model;

    }
}

如果我手动设置 IsSelected 属性,则 SelectedTimesheets lambda 可以工作,因此问题出在 XAML 与 IsSelected 属性的绑定中。

任何帮助,将不胜感激。

4

2 回答 2

6

当然,我知道你的意思。太糟糕了,这不是自动的,但它不是。该解决方案涉及一个简单的自定义GridView,它继承自GridView. 没什么太疯狂的,也就是说,如果你让它沉入其中。这是代码,我只是测试了它:

这是您的 XAML:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.ColumnDefinitions >
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <local:MyGridView ItemsSource="{Binding Items}" SelectionMode="Multiple"
                      BindableSelectedItems="{Binding Selected}" />
    <local:MyGridView Grid.Column="1" ItemsSource="{Binding Selected}" />
</Grid>

这是您的视图模型(超级简化):

public class ViewModel
{
    ObservableCollection<string> m_Items
        = new ObservableCollection<string>(Enumerable.Range(1, 100).Select(x => x.ToString()));
    public ObservableCollection<string> Items { get { return m_Items; } }

    ObservableCollection<object> m_Selected = new ObservableCollection<object>();
    public ObservableCollection<object> Selected { get { return m_Selected; } }
}

这是您的自定义网格视图:

public class MyGridView : GridView
{
    public ObservableCollection<object> BindableSelectedItems
    {
        get { return GetValue(BindableSelectedItemsProperty) as ObservableCollection<object>; }
        set { SetValue(BindableSelectedItemsProperty, value as ObservableCollection<object>); }
    }
    public static readonly DependencyProperty BindableSelectedItemsProperty =
        DependencyProperty.Register("BindableSelectedItems",
        typeof(ObservableCollection<object>), typeof(MyGridView),
        new PropertyMetadata(null, (s, e) =>
        {
            (s as MyGridView).SelectionChanged -= (s as MyGridView).MyGridView_SelectionChanged;
            (s as MyGridView).SelectionChanged += (s as MyGridView).MyGridView_SelectionChanged;
        }));
    void MyGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (BindableSelectedItems == null)
            return;
        foreach (var item in BindableSelectedItems.Where(x => !this.SelectedItems.Contains(x)).ToArray())
            BindableSelectedItems.Remove(item);
        foreach (var item in this.SelectedItems.Where(x => !BindableSelectedItems.Contains(x)))
            BindableSelectedItems.Add(item);
    }
}

只是一个新的财产BindableSelectedItems

祝你好运!

于 2013-08-22T05:54:18.890 回答
1

@Jerry-Nixon-MSFT 的回答促使我重新思考(感谢他),我想出了以下解决方案。

首先,我更改了 XAML 以接受新的辅助方法SelectionChangedCommand.Command并将其绑定到我的视图模型中名为SelectionChangedCommand的RelayCommand

主页.xaml

<GridView 
    x:Name="itemGridView"
    IsItemClickEnabled="True"
    ItemsSource="{Binding Timesheets}"
    ItemTemplate="{StaticResource TimesheetTemplate}" 
    Margin="10"
    Grid.Column="0"
    SelectionMode="Multiple"
    helpers:ItemClickCommand.Command="{Binding NavigateTimesheetCommand}" 
    helpers:SelectionChangedCommand.Command="{Binding SelectionChangedCommand}
    "RenderTransformOrigin="0.738,0.55"  >
</GridView>

然后,我在助手命名空间下添加了一个SelectionChangedCommand助手类,以将SelectionChanged事件转换为ICommand

namespace TimesheetManager.Helpers
{
    public class SelectionChangedCommand
    {
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand),
            typeof(SelectionChangedCommand), new PropertyMetadata(null, 
            OnCommandPropertyChanged));

        public static void SetCommand(DependencyObject d, ICommand value)
        {
            d.SetValue(CommandProperty, value);
        }

        public static ICommand GetCommand(DependencyObject d)
        {
            return (ICommand)d.GetValue(CommandProperty);
        }

        private static void OnCommandPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            var control = d as ListViewBase;

            if (control != null)
                control.SelectionChanged += OnSelectionChanged;
        }

        private static void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var control = sender as ListViewBase;
            var command = GetCommand(control);

            if (command != null && command.CanExecute(e))
                command.Execute(e);

        }
    }

}

这会将继承自ListViewBase (我们的网格视图)的任何控件的SelectionChanged事件绑定到名为OnSelectionChanged的​​方法。OnSelectionChanged随后将SelectionChangedEventArgs从控件传递到 XAML 中的RelayCommand绑定。

最后在MainViewModel中,我处理RelayCommand并设置IsSelected标志:

主视图模型:

private RelayCommand<object> _selectionChangedCommand;

/// <summary>
/// Gets the SelectionChangedCommand.
/// </summary>
public RelayCommand<object> SelectionChangedCommand
{
    get
    {
        return _selectionChangedCommand ?? (_selectionChangedCommand = new RelayCommand<object>
        ((param) => ExecuteSelectionChangedCommand(param)));
    }
}

private void ExecuteSelectionChangedCommand(object sender)
{
    var x = sender as SelectionChangedEventArgs;

    foreach (var item in x.AddedItems)
            ((TimesheetViewModel)item).IsSelected = true;

    foreach (var item in x.RemovedItems)
            ((TimesheetViewModel)item).IsSelected = false;

}

我知道正在进行大量的转换,但我们仅限于ICommand接口的对象。

希望这可以帮助。

于 2013-08-22T16:35:51.623 回答