繁体   English   中英

UWP Gridview 绑定到 ObservableCollection 并清除值

[英]UWP Gridview Binding to ObservableCollection and clearing values

所以我在绑定到 ObservableCollection 的 UWP 应用程序中创建了一个 GridView。

XAML:

<Page.Resources>
    <CollectionViewSource
            x:Name="ListSource"
            Source="{x:Bind model.ShowList}"
            IsSourceGrouped="false"
            />
</Page.Resources>
...
<Page>
    <GridView x:Name="lvListSource" ItemsSource="{Binding Source={StaticResource ListSource}}" SelectedIndex="-1" Grid.Row="2" HorizontalContentAlignment="Center" SelectionChanged="lvEpisodeListSource_SelectionChanged">
                <GridView.ItemTemplate>
                    <DataTemplate x:DataType="local:PageInput">
                        <TextBlock Name="AlbumBlock" Foreground="Black" FontWeight="Normal" FontSize="15" Margin="5,0,5,0"
                                    Text="{x:Bind Name}" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100"/>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
</page>

C#:

public MainCategoryModel model;
public MainPage()
{
    model = new MainCategoryModel();
    this.InitializeComponent();
}

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    //initialize model data in code and stuff comes out correctly
    //model.Clear() give unknown error here
    model.Add(new PageInput() {...});
    lvListSource.ItemsSource = model.myList;
}

public class MainCategoryModel
{
    public ObservableCollection<PageInput> myList { get; set; }

    public MainCategoryModel()
    {
        myList = new ObservableCollection<PageInput>();
    }

    public void AddShow(PageInput show)
    {
        myList.Insert(0, show);
    }

    public void Clear()
    {
        myList.Clear();
    }
}

ObservableCollection 正确初始化并且当我只向集合添加项目时页面加载非常好。 问题是,每次调用 OnNavigatedTo 函数时,我都想更新 GridView 绑定到的 ObservableCollection。 这包括从集合中删除项目。 每次我删除项目或尝试清除 ObservableCollection 时,页面都无法加载并出现未知错误。 有没有办法从已经绑定的 Observable 集合中修改和删除值?

作为一个附带问题,如果我不在 OnNavigatedTo 的末尾重新设置 ItemsSource 属性,则在将值添加到集合后会立即调用 SelectionChanged 事件,因为它是在 XAML 中设置的。 有没有办法在将项目添加到 GridView 时不调用 SelectionChanged 事件?

x:Bind 默认为 OneTime,需要设置 Mode=OneWay 来获取布局更新

   protected override void OnNavigatedTo(NavigationEventArgs e)
   {
       //initialize model data in code and stuff comes out correctly

      lvEpisodeListSource.ItemsSource = model.myList;
   }

杀死您的绑定,更新 model.myList 并让绑定完成工作,如果您希望绑定它,请不要以这种方式从代码中设置 ItemsSource

编辑:

复制并运行您的代码(这不是愉快的体验,因为您没有将其清理干净)并且我不明白为什么您的代码中的 Binding 和 x:Bind 如此混乱,您为什么不直接绑定这样的收藏?

<GridView x:Name="lvListSource" ItemsSource="{x:Bind model.myList, Mode=OneWay}" ....

编辑2:

我的代码:

public class MainCategoryModel
{
    public ObservableCollection<PageInput> myList { get; set; }

    public MainCategoryModel()
    {
        myList = new ObservableCollection<PageInput>();
    }

    public void AddShow(PageInput show)
    {
        myList.Insert(0, show);
    }

    public void Clear()
    {
        myList.Clear();
    }
}

<Page>
(........)
    <GridView x:Name="lvListSource" ItemsSource="{x:Bind model.myList, Mode=OneWay}" SelectedIndex="-1" Grid.Row="2" HorizontalContentAlignment="Center">
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="local:PageInput">
            <TextBlock Name="AlbumBlock" Foreground="Black" FontWeight="Normal" FontSize="15" Margin="5,0,5,0"
                                Text="{x:Bind Name}" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100"/>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

public sealed partial class MainPage : Page
{
    public MainCategoryModel model;
    public MainPage()
    {
        model = new MainCategoryModel();
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        model.Clear();
        model.myList.Add(new PageInput() {Name = "testName"});
    }

}

由 ques 制作:

public class PageInput
{
    public string Name { get; set; }
}

尝试运行并检查它

打开页面缓存时,清除 XAML 代码绑定到的数据对象对象会出现未知错误。 禁用页面缓存可让您正确清除对象。

this.NavigationCacheMode = NavigationCacheMode.Disabled;

这是社区中已有的示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM