2016-04-21 31 views
0

我已经实现了在SingleMultiple之间切换的功能ListView的选择。我创建了触发该开关的AppBarButton。除了刷新CollecitonViewSource以外,一切正常。CollectionViewSource未在AppbarButtonPress上更新

在beckend数据被删除一切正常,但ListView不显示新的数据。

private void MultipleSelectionMode_Click(object sender, RoutedEventArgs e) { 
    if (ContactsListView.SelectionMode == ListViewSelectionMode.Single) { 
     //change selection mode to multiple 
     ContactsListView.SelectionMode = ListViewSelectionMode.Multiple; 

     //change appbar button icon 
     MultipleSelectionMode.Icon = new SymbolIcon(Symbol.Delete); 
    } 
    else if (ContactsListView.SelectionMode == ListViewSelectionMode.Multiple) { 
     //remove selected items 
     List<object> selectedContacts = ContactsListView.SelectedItems.ToList(); 

     foreach (Person person in selectedContacts) { 
      //remove contact from database 
      GlobalData.RemoveContact(person); 
     } 

     //change selection mode to single 
     ContactsListView.SelectionMode = ListViewSelectionMode.Single; 

     //change appbar button icon 
     MultipleSelectionMode.Icon = new SymbolIcon(Symbol.Bullets); 

     //new data are in groupingItems but not displayed on the screen 
     groupingItems = Person.createGrouping(GlobalData.LoadContacts()); 
    } 
} 

当按下AppBarButton时调用此方法。 ContactsListView是这样定义的ListView

<ListView 
    x:Name="ContactsListView"  
    ItemTemplate="{StaticResource ContactsTemplate}" 
    SelectionMode="Single" 
    ItemsSource="{x:Bind ContactsViewSource.View}" 
    Grid.Row="1"> 
    <ListView.GroupStyle> 
     <GroupStyle> 
      <GroupStyle.HeaderTemplate> 
       <DataTemplate x:DataType="data:GroupingItem"> 
        <TextBlock Text="{x:Bind Key}" 
            Foreground="Blue"/> 
       </DataTemplate> 
      </GroupStyle.HeaderTemplate> 
     </GroupStyle> 
    </ListView.GroupStyle> 
</ListView> 

CollectionViewSources看起来是这样的:

<CollectionViewSource 
    x:Name="ContactsViewSource" 
    x:Key="src" 
    Source="{x:Bind groupingItems, Mode=OneWay}" 
    IsSourceGrouped="True" /> 

我知道这是工作,因为我打电话groupingItems = Person.createGrouping(GlobalData.LoadContacts());多次绑定和数据在每个被刷新案件。它不能只按AppBarButton

你知道为什么会发生这种情况吗?

+0

你期待什么结果?因为对我来说,'selectedContacts'似乎是空的,所以你的数据不会改变。原因是你在单一模式和多选模式之间切换时调用了这个代码,但是你应该在选择的项目改变时调用它。 – StepTNT

+0

@StepTNT我编辑了这个问题来回答你的评论。 selectedContacts不为空,从数据库中删除数据并加载新数据。但是这个观点并没有被刷新。 – miskohut

回答

0

如果要发回用户所做的更改,则必须在使用x:bind时使用双向模式。

Source="{x:Bind groupingItems, Mode=TwoWay}" 

X:绑定默认的模式是一个时间,如果使用双向你要告诉控制更新每次列表视图用户变化的东西在UI的源数据。

,你需要使用一个ObservableCollection,而不是一个列表

编辑

public class BindableSourceConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, string language) 
     { 
      return value; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, string language) 
     { 
      return value; 
     } 
    } 

使用该转换工具,利用的ObservableCollection

+0

其实这是我已经尝试,与编译错误的事情:无效绑定路径'groupingItems':无法将类型'System.Collections.ObjectModel.ObservableCollection(People.Backend.GroupingItem)'绑定到'System.Object'没有转换器。 – miskohut

+0

是的,你需要一个转换器,我要用一个工作转换器来更新答案 – frenk91

相关问题