0

下面是用于处理我的 gridview 中选定项目的 xaml 和 c# 代码。

我也在使用 MVVM Light,一切正常,包括我能够看到 SelectedItems 内部的内容。

但是,当我尝试清除 SelectedItems 时,我的 UI 似乎没有更新/反映对 SelectedItems 所做的更改。

我正在使用 WinRT XAML 工具包 ( http://winrtxamltoolkit.codeplex.com/ ),它在 GridView 上具有 BindableSelection 扩展

XAML

<controls:CustomGridView
    x:Name="VideoItemGridView"
    Grid.Row="2"
    Margin="0,-3,0,0"
    Padding="116,0,40,46"
    HorizontalContentAlignment="Stretch"
    VerticalContentAlignment="Stretch"
    IsItemClickEnabled="True"
    SelectionMode="Extended"
    Extensions:GridViewExtensions.BindableSelection="{Binding SelectedVideoItems, Mode=TwoWay}"
    ItemsSource="{Binding Source={StaticResource ViewSource}}"
    ItemTemplate="{StaticResource VideoItemTemplate}">
    <GridView.ItemsPanel>
        <ItemsPanelTemplate>
            <VariableSizedWrapGrid ItemWidth="250" ItemHeight="160" />
        </ItemsPanelTemplate>
    </GridView.ItemsPanel>
</controls:CustomGridView>

MyViewViewModel.cs

#region Selected Items

/// <summary>
/// Gets or sets the selected video items.
/// </summary>
public ObservableCollection<object> SelectedVideoItems
{
    get { return this._selectedVideoItems; }
    set
    {
        this._selectedVideoItems = value;
        this.Set("SelectedVideoItems", ref this._selectedVideoItems, value);
    }
}
private ObservableCollection<object> _selectedVideoItems = new ObservableCollection<object>();

#endregion

#region App Bar Click Commands

/// <summary>
/// Gets the ClearSelection click command.
/// </summary>
public ICommand ClearSelectionClickCommand
{
    get
    {
        return new RelayCommand(() => this.ClearSelectionOperation());
    }
}

/// <summary>
/// Selects all command operation.
/// </summary>
private void ClearSelectionOperation()
{
    this.SelectedVideoItems = new ObservableCollection<object>();
}

#endregion
4

2 回答 2

0

事实证明,由于我使用的是数据模板,实际上是我的数据模型需要设置一个标志来指示它被选中

这是拼图中缺失的部分。一旦我更新了绑定到网格视图项的数据模型(其中还包括对行/列跨越的支持),UI 就会按预期更新。

希望这对其他人有帮助。

public class CustomGridView : GridView
{
    protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    {
        try
        {
            base.PrepareContainerForItemOverride(element, item);

            dynamic _Item = item;
            element.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, _Item.ColumnSpan);
            element.SetValue(VariableSizedWrapGrid.RowSpanProperty, _Item.RowSpan);
            element.SetValue(GridViewItem.IsSelectedProperty, _Item.IsSelected);
        }
        catch
        {
            element.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, 1);
            element.SetValue(VariableSizedWrapGrid.RowSpanProperty, 1);
            element.SetValue(GridViewItem.IsSelectedProperty, false);
        }
        finally
        {
            base.PrepareContainerForItemOverride(element, item);
        }
    }
于 2013-03-30T02:54:18.643 回答
0

ClearSelectionOperation尝试通过调用清除您选择的项目

this.SelectedVideoItems.Clear();

代替

this.SelectedVideoItems = new ObservableCollection<object>();

如果这无济于事,请检查3 月 7 日的当前版本的扩展是否解决了问题。

于 2013-03-27T00:18:59.190 回答