【问题标题】:data binding content refreshing on pivotitem page在pivotitem页面上刷新数据绑定内容
【发布时间】:2014-04-13 13:01:18
【问题描述】:

我开发了一个应用程序,但遇到以下部分的问题。

我有一个“文本块”,它绑定了可观察项目集合中的确切项目(“项目”)并显示数字。每个项目都有一个属性,称为“计数”。

当用户点击当前文本块时,它位于 longlistselector 的堆栈面板中(绑定项目),我需要这个文本块来显示新的数字(想法是每次用户点击文本块,数字增加 1)。

我发现的唯一方法是每次导航到当前枢轴项目(文本块所在的位置)以强制枢轴项目从可观察集合重新加载数据,其中存储更新的项目(和“计数”属性)。但它很慢而且效果不好。

请建议每次用户点击当前文本块时如何使数字出现在文本块中。

<phone:PivotItem Name="Documents" Header="Documents" Margin="0" >
                <Grid>
                    <phone:LongListSelector ItemsSource="{Binding Items}" >
                        <phone:LongListSelector.ItemTemplate>
                            <DataTemplate>


                                    <StackPanel  Orientation="Horizontal"
                                        Height="95" >


                                        <TextBlock Tap="Plus_Tap" Visibility="Visible" Width="20" Height="50" Text="{Binding Count}" FontFamily="Segoe WP Light"
                                                   FontSize="35" Foreground="White" Margin="2,0,0,2"/>

                                    </StackPanel>

                                </StackPanel>

                            </DataTemplate>

                        </phone:LongListSelector.ItemTemplate>


                    </phone:LongListSelector>


                </Grid>
            </phone:PivotItem>
namespace PackMan
{
    public partial class CategoryList : PhoneApplicationPage
    {
        public CategoryList()
        { 
            InitializeComponent();
            DataContext = App.ViewModel;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
        private void Plus_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            PackList selectedList = (sender as TextBlock).DataContext as PackList;
            selectedList.Count += 1;

            NavigationService.Navigate(new Uri("/CategoryList.xaml?Documents", UriKind.Relative));

        }
    }
}

How to refresh the data in pivot item after updating data through xaml UI?

这里有一个人在他对自己问题的发布答案的最后写道:“如果您使用 observableCollection,则无需刷新页面。这就是 ViewModel 的魔力。”但是我从“Items”可观察集合中加载数据,该集合不在 ViewModel 中,而是在 Packlist.cs 中声明。也许这会有所帮助。

这里我确实增加了 ObservableCollection "Items" 中项目的 Count 属性:

private void Plus_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            PackList selectedList = (sender as TextBlock).DataContext as PackList;
            selectedList.Count += 1;

我点击文本块,文本块的文本没有显示任何变化。我点击返回 - 这让我回到菜单页面,然后我再次打开数据透视项目页面,并看到文本块的文本(数字)增加了 1。只有在我重新加载页面(数据透视项)时才能看到更改,因为我在它的构造函数和 OnNavigatedTo 方法中有这个:

 public CategoryList()
        { 
            InitializeComponent();
            DataContext = App.ViewModel;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
LoadData() - loads OBservable collection ("Items"), which has items, and Count as one   of its properties:

public void LoadData()
        {
            this.Items = LoadNewLists();
            this.IsDataLoaded = true;
        }

也许现在您可以告诉我如何在我点击文本块时立即更改文本块的文本(文本块)。我会感谢您的任何建议,因为这一刻让我无法在第二天开发我的应用程序。

【问题讨论】:

    标签: pivotitem


    【解决方案1】:

    我想通了。我应该已经阅读了 INotifyPropertyChanged 接口并使用此接口的方法创建了“计数”属性:

     public int Count
        {
            get
            {
                return _count;
            }
            set
            {
                if (value != _count)
                {
                    _count = value;
                    NotifyPropertyChanged("Count");
                }
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    

    希望它对任何人都有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 2012-09-17
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多