简体   繁体   中英

Reload LongListSelector with updated data from local database - Windows Phone 8

I am having an issue with a longlistselector on my Windows Phone 8 application. After selecting and editing a record from the longlistselector and submitting the changes to my local database, the longlistselector doesn't show the updated name,unless you close and reopen the application. The data is definitely being updated, its just not showing. Is there a way to have the list select while the app is still open? This is the Xaml for the list:

 <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <phone:LongListSelector x:Name="llsModules"
                               Margin="0,0,-12,0"
                               ItemsSource="{Binding Modules}"
                               SelectionChanged="llsModules_SelectionChanged">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17">
                        <StackPanel Orientation="Horizontal">
                            <Grid HorizontalAlignment="Stretch" Width="420"  >                             
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="100"/>
                                    <ColumnDefinition Width="100"/>
                                </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding Name,StringFormat='Name: {0}'}"
                                TextWrapping="Wrap"
                                MaxWidth="300"
                                Style="{StaticResource PhoneTextLargeStyle}"
                                       HorizontalAlignment="Left"
                                       Grid.Row="1"
                                       />
                            </Grid>
                        </StackPanel>

And this is the code behind.

public ModulePage()
    {
        InitializeComponent();          
        DataContext = App.ViewModel;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        App.ViewModel.LoadModulesData();
        llsModules.ItemsSource = App.ViewModel.Module;            
    }

    private void llsModules_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (llsModules.SelectedItem == null)
            return;

        NavigationService.Navigate(new Uri("/ModuleDetails.xaml?moduleid=" + (llsModules.SelectedItem as Modules).Id, UriKind.Relative));

    }

And this is the data model

 [Table(Name = "Modules")]
    public class Modules : INotifyPropertyChanged, INotifyPropertyChanging
    {
        public Modules()
        {
        }

        private int _id;
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                if (_id != value)
                {
                    NotifyPropertyChanging("Id");
                    _id = value;
                    NotifyPropertyChanged("Id");
                }
            }
        }

        private string _name;
        [Column(DbType = "nvarchar(255)", CanBeNull = false)]
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value == "")
                    throw new Exception("Module Name is a required field");

                if (_name != value)
                {
                    NotifyPropertyChanging("Name");
                    _name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

Thanks so much!

In Xaml change the binding which is used to display the name to TwoWay .

Text="{Binding Name, Mode=TwoWay, StringFormat='Name: {0}'}"

This lets the xaml know to monitor the InotifyPropertyChanges of the Name property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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