UWP GridView与观测值和清除值结合



因此,我正在UWP应用程序中创建一个绑定到观测值collection的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();
    }
}

观察力计算正确初始初始化,并且当我仅在集合中添加项目时,页面可以完全加载。问题是,我想更新此观测值collection,每次调用onnavigigatedto函数时,GridView都绑定到。这包括从集合中删除项目。每次我删除项目或尝试清除观察力授权时,页面都不会加载未知错误。是否可以从已经绑定到的可观察到的集合中修改和删除值?

作为侧面问题,如果我不在onnavigatedto的末尾重新设置itempssource属性,则在XAML中设置了该集合值之后,选择变成的事件将立即调用。有没有办法将项目添加到GridView时,将选择变为事件不调用?

x:默认情况下,bind是符合时间的,您需要设置IT模式= oneway才能获取布局更新

   protected override void OnNavigatedTo(NavigationEventArgs e)
   {
       //initialize model data in code and stuff comes out correctly
      lvEpisodeListSource.ItemsSource = model.myList;
   }

杀死您的绑定,更新模型。

编辑:

复制并运行您的代码(这不是令人愉快的经历,因为您没有使其干净),并且我不散发和为什么对绑定和x:bind bind bint your code却不您只是绑定这样的收藏?

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

edit2:

我的代码:

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

}

,由Quess制作:

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

尝试运行并检查

清除XAML代码在打开页面缓存时必须给出未知错误的数据对象。禁用页面缓存允许您正确清除对象。

this.NavigationCacheMode = NavigationCacheMode.Disabled;

这是一个已经在社区中的示例

最新更新