问题描述
所以我要在绑定到ObservableCollection的UWP应用中创建GridView.
so I am creating a GridView in a UWP app which is bound to an ObservableCollection.
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集合中修改和删除值?
The ObservableCollection initializes properly and the page loads perfectly fine when I only add items to the collection. The problem is, I would like to update this ObservableCollection the GridView is bound to every time the OnNavigatedTo function is called. This includes removing items from the collection. Every time I either remove items or try to Clear the ObservableCollection the page fails to load with an unknown error. Is there a way to modify and remove values from an Observable Collection that is already bound to?
作为一个附带的问题,如果我没有在OnNavigatedTo的末尾重新设置ItemsSource属性,则将值添加到Collection之后会立即调用SelectionChanged事件,因为它是在XAML中设置的.将项目添加到GridView时,是否有一种方法可以使SelectionChanged事件不被调用?
As a side problem, If I don't re-set the ItemsSource property at the end of OnNavigatedTo, the SelectionChanged event is called immediately after adding values to the Collection since it is set in XAML. Is there a way to have the SelectionChanged event not call when items are added to a GridView?
推荐答案
x:默认情况下,绑定是OneTime,您需要将其设置为Mode = OneWay以获取布局更新
x:Bind is OneTime by default, you need to set it Mode=OneWay to get the layout updates
protected override void OnNavigatedTo(NavigationEventArgs e)
{
//initialize model data in code and stuff comes out correctly
lvEpisodeListSource.ItemsSource = model.myList;
}
杀死您的绑定,更新model.myList并让绑定完成任务,如果您希望将其绑定,请不要通过代码设置ItemsSource
kills your binding, update model.myList and let the binding do the job, don't set ItemsSource from code this way if you want it to be binded
复制并运行您的代码(这不是令人愉快的经历,因为您没有把它弄干净),我也不明白为什么您的代码中的Binding和x:Bind如此混乱,为什么不你只是这样绑定收藏?
copied and run your code (which wasn't pleasant experience because you didn't make it clean) and I don't undersand why there is such mess with the Binding and x:Bind in your code, why don't you just bind the collection like that?
<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; }
}
尝试运行并检查它
这篇关于UWP Gridview绑定到ObservableCollection并清除值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!