【问题标题】:Collection Bind to expanded View won't update when new Items are added添加新项目时,集合绑定到扩展视图不会更新
【发布时间】:2012-01-31 14:03:29
【问题描述】:

我在我的 MainPage 中使用了一个扩展器视图,它绑定到“帐户类别”集合(此集合中的每个项目都有一个帐户集合)

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

绑定在主页的 OnNavigatedTo 事件上完成 数据库上下文文件由 Sql Metal 工具生成 (更多信息在这里Using SQl Metal to generate DB files for 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>

关于什么是错的任何想法? 提前感谢您的帮助..

【问题讨论】:

    标签: windows-phone-7 binding sqlmetal


    【解决方案1】:

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

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

    【讨论】:

    • “确保将更新的数据保存/标记到“添加新帐户”页面中的上下文中”我将数据保存在“添加新帐户”页面上。但是当我导航回主页时,新输入的记录是不可见的..
    • 伙计们,请在这里帮忙..它现在有点让我头疼..:(谢谢
    • 查看我之前的评论:向我们展示在“添加新帐户”页面中保存数据的代码。
    猜你喜欢
    • 2011-05-25
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多