0

以下是用于处理GridView中选定项目的xaml和c#代码。WinRT XAML工具包BindableSelections不更新用户界面

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

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

我使用这对一个GridView

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的BindableSelection扩展

#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 
+0

您是从源代码还是NuGet软件包使用当前版本的工具包?最近(3月7日)还有一些更新尚未进入新的NuGet软件包。 – 2013-03-27 00:16:46

回答

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); 
     } 
    }