UWP Gridview绑定到ObservableCollection并清除值

时间:2016-12-07 16:33:15

标签: c# gridview uwp

所以我在一个绑定到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正确初始化并且页面加载完全正常。问题是,我想更新这个ObservableCollection,GridView绑定到每次调用OnNavigatedTo函数时。这包括从集合中删除项目。每次我删除项目或尝试清除ObservableCollection时,页面都无法加载未知错误。有没有办法修改和删除已绑定到的Observable Collection中的值?

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

2 个答案:

答案 0 :(得分:0)

x:默认情况下绑定为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有这样的混乱:在代码中绑定,为什么不你只是绑定那个集合?

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

尝试运行并检查

答案 1 :(得分:0)

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

this.NavigationCacheMode = NavigationCacheMode.Disabled;