添加新项目时,集合绑定到扩展视图不会更新

发布于 2024-12-29 18:57:42 字数 4588 浏览 2 评论 0原文

我在主页中使用扩展器视图,该视图绑定到“帐户类别”集合(该集合中的每个项目还有一个帐户集合)。

绑定一切正常,但有一个小故障。还有另一个页面,用户可以在其中添加新帐户(从而更改帐户和帐户类别),现在当我导航回主页时,扩展器控件不显示更新的值?

绑定是在主页的 OnNavigateTo 事件上完成的 DataBase上下文文件由Sql Metal工具生成 (更多信息请参见使用 SQl Metal为 wp7 生成数据库文件

这意味着我的所有类都实现了 INotifyChanging & INotifyChangedEvents

这是 XAML & C# 代码

private WalletDataContext context;

    private ObservableCollection<AccountCategory> _accountCategories;
    public ObservableCollection<AccountCategory> AccountCategories
    {
        get { return _accountCategories; }
        set
        {
            if (_accountCategories != value)
            {
                _accountCategories = value;
                NotifyPropertyChanged("AccountCategories");
            }
        }
    }

public MainPage()
    {
        InitializeComponent();

        //Initialize Data context
        context = new WalletDataContext(WalletDataContext.DBConnectionString);

        //Set page data context
        this.DataContext = this;
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        //fetch all existing Account Categories
        var accountCategoriesInDB = (from AccountCategory acctCat in context.AccountCategory
                                     select acctCat);

        //Update Page Collection
        AccountCategories = new ObservableCollection<AccountCategory>(accountCategoriesInDB);

        this.listBox.ItemsSource = AccountCategories;

        base.OnNavigatedTo(e);

    }

以下是 XAML 绑定

<ListBox Grid.Row="0" x:Name="listBox">
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        </Style>
                    </ListBox.ItemContainerStyle>

                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <toolkit:ExpanderView Header="{Binding}" Expander="{Binding}"
                            ItemsSource="{Binding Accounts}"
                            HeaderTemplate="{StaticResource CustomHeaderTemplate}" ExpanderTemplate="{StaticResource CustomExpanderTemplate}">
                                <toolkit:ExpanderView.ItemTemplate>
                                    <DataTemplate>
                                        <Grid VerticalAlignment="Center" Height="Auto">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="0.105*"/>
                                                <RowDefinition Height="0.105*"/>
                                                <RowDefinition Height="0.789*"/>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="0.366*"/>
                                                <ColumnDefinition Width="0.634*"/>
                                            </Grid.ColumnDefinitions>

                                            <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding AccountNumber}" Style="{StaticResource PhoneTextNormalStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBlock>
                                            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Balance}" Style="{StaticResource PhoneTextSubtleStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBlock>
                                        </Grid>
                                    </DataTemplate>
                                </toolkit:ExpanderView.ItemTemplate>
                            </toolkit:ExpanderView>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

对于问题有什么想法吗? 感谢您提前的帮助..

I am using a Expander View in my MainPage that is Bind to a collection of "Account Categories" (each item in this collection has further a collection of Accounts)

The Bindings are all working fine, with a small glitch though. There is another page Where a user can Add new Accounts (Thus changing Accounts & Account Categories) now when I navigate back to the Main Page the Expander Control does not show updated values?

The binding is done on OnNavigatedTo event of the Main Page
The DataBase context file is generated by Sql Metal tool
(More on this here Using SQl Metal to generate DB files for wp7)

That means all my classes implement INotifyChanging & INotifyChangedEvents

Here is the XAML & C# code

private WalletDataContext context;

    private ObservableCollection<AccountCategory> _accountCategories;
    public ObservableCollection<AccountCategory> AccountCategories
    {
        get { return _accountCategories; }
        set
        {
            if (_accountCategories != value)
            {
                _accountCategories = value;
                NotifyPropertyChanged("AccountCategories");
            }
        }
    }

public MainPage()
    {
        InitializeComponent();

        //Initialize Data context
        context = new WalletDataContext(WalletDataContext.DBConnectionString);

        //Set page data context
        this.DataContext = this;
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        //fetch all existing Account Categories
        var accountCategoriesInDB = (from AccountCategory acctCat in context.AccountCategory
                                     select acctCat);

        //Update Page Collection
        AccountCategories = new ObservableCollection<AccountCategory>(accountCategoriesInDB);

        this.listBox.ItemsSource = AccountCategories;

        base.OnNavigatedTo(e);

    }

Here are the XAML bindings

<ListBox Grid.Row="0" x:Name="listBox">
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        </Style>
                    </ListBox.ItemContainerStyle>

                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <toolkit:ExpanderView Header="{Binding}" Expander="{Binding}"
                            ItemsSource="{Binding Accounts}"
                            HeaderTemplate="{StaticResource CustomHeaderTemplate}" ExpanderTemplate="{StaticResource CustomExpanderTemplate}">
                                <toolkit:ExpanderView.ItemTemplate>
                                    <DataTemplate>
                                        <Grid VerticalAlignment="Center" Height="Auto">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="0.105*"/>
                                                <RowDefinition Height="0.105*"/>
                                                <RowDefinition Height="0.789*"/>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="0.366*"/>
                                                <ColumnDefinition Width="0.634*"/>
                                            </Grid.ColumnDefinitions>

                                            <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding AccountNumber}" Style="{StaticResource PhoneTextNormalStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBlock>
                                            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Balance}" Style="{StaticResource PhoneTextSubtleStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBlock>
                                        </Grid>
                                    </DataTemplate>
                                </toolkit:ExpanderView.ItemTemplate>
                            </toolkit:ExpanderView>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

Any ideas on whats wrong?
Thanks for your help in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

MainPage 在 OnNavigateTo 方法中重新加载上下文中的数据。

确保将更新的数据保存/标记到“添加新帐户”页面的上下文中。

The MainPage reloads the data from the context in the OnNavigatedTo method.

Make sure you save/mark the updated data to the context in the Add New Account page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文