【问题标题】:WinRT XAML Toolkit BindableSelections not updating UIWinRT XAML 工具包 BindableSelections 未更新 UI
【发布时间】:2013-03-26 22:38:18
【问题描述】:

以下是用于处理我的 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

【问题讨论】:

  • 您使用的是源代码中的当前版本的工具包还是 NuGet 包?最近(3 月 7 日)有一些更新尚未进入新的 NuGet 包。

标签: xaml windows-runtime winrt-xaml mvvm-light winrt-xaml-toolkit


【解决方案1】:

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

this.SelectedVideoItems.Clear();

而不是

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

如果这不能帮助检查current version of the extension from March 7 是否解决了问题。

【讨论】:

    【解决方案2】:

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

    这是拼图中缺失的部分。一旦我更新了绑定到网格视图项的数据模型(其中还包括对行/列跨越的支持),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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多