【问题标题】:Auto bind data to grid.textblock自动将数据绑定到 grid.textblock
【发布时间】:2014-04-26 22:08:02
【问题描述】:

我有一个问题,我不知道如何在 windows phone 8 中绑定数据,场景是:-

我从服务器请求数据,得到响应后,我将它们显示在网格和列表框中(如表格)。
在我完成显示功能后,直到这里一切都好,有一个新功能可以检索对最后一个请求的更新。

这是我的问题:
当我收到新数据时,我不知道如何将这些新数据绑定到同一个旧表。
例如:第一个请求是返回黄金价格1500$--->我在表格中显示--->然后新请求-->更新函数返回黄金的新价格是1502$。
如何在应用程序运行时使用新价格更新具有黄金价格文本块的所需行。

这是第一个请求:-

public ObservableCollection<Data> DataReceivedCollection { get; set; }

private void FireRequest2()
    {

        var request = HttpWebRequest.Create(new Uri("http://74.54.46.178/vertexweb10/webservice.svc/getallsymbols?AccountID=1122336675")) as HttpWebRequest;
        request.Method = "GET";
        request.CookieContainer = cookieJar;
        request.BeginGetResponse(ar =>
        {
            HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState;
            using (var response = (HttpWebResponse)req2.EndGetResponse(ar))
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var outerRoot1 = JsonConvert.DeserializeObject<OuterRootObject1>(reader.ReadToEnd());
                        JArray jsonArray = JArray.Parse(outerRoot1.d);
                        JToken jsonArray_Item = jsonArray.First;
                        while (jsonArray_Item != null)
                        {

                            string Name = jsonArray_Item.Value<string>("Name");
                            string Bid = jsonArray_Item.Value<string>("Bid");
                            string Ask = jsonArray_Item.Value<string>("Ask");
                            string ID = jsonArray_Item.Value<string>("ID");



                            DataReceivedCollection = new ObservableCollection<Data>();


                            DispatchInvoke(() =>
                            {
                                myList.ItemsSource = DataReceivedCollection;
                                // and to add data you do it like this:
                                DataReceivedCollection.Add(new Data() { symid = ID, textFirst = Name, textSecond = Bid, textThird = Ask });


                            }
);

                            //Be careful, you take the next from the current item, not from the JArray object.
                            jsonArray_Item = jsonArray_Item.Next;
                        }



                    }
                }

            }

        }, request);
    }

这是我的 XAML,我想从 firerequest2() 中删除请求的数据;

<Grid Background="#FFC9DC97" x:Name="ContentPanel" Grid.Row="1" Margin="12,140,12,0">
        <ListBox Name="myList" Background="#FFC9DC97">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*"/>
                            <ColumnDefinition  Width="1*"/>
                            <ColumnDefinition Width="1*"/>
                            <ColumnDefinition Width="1*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="ide" Text="{Binding symid}" Grid.Column="3" HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding textFirst}" Grid.Column="0" HorizontalAlignment="Left" Foreground="#FF1C69D8"/>
                        <TextBlock Text="{Binding textSecond}" Grid.Column="1" HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding textThird}" Grid.Column="2" HorizontalAlignment="Right"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

到这里一切正常,我不知道用下一个函数的新数据更新网格

public class Data : INotifyPropertyChanged
    {
        private string _textFirst;
        public string textFirst
        {
            [DebuggerStepThrough]
            get { return _textFirst; }
            [DebuggerStepThrough]
            set
            {
                if (value != _textFirst)
                {
                    _textFirst = value;
                    OnPropertyChanged("textFirst");
                }
            }
        }
        private string _textSecond;
        public string textSecond
        {
            [DebuggerStepThrough]
            get { return _textSecond; }
            [DebuggerStepThrough]
            set
            {
                if (value != _textSecond)
                {
                    _textSecond = value;
                    OnPropertyChanged("textSecond");
                }
            }
        }

        private string _textThird;
        public string textThird
        {
            [DebuggerStepThrough]
            get { return _textThird; }
            [DebuggerStepThrough]
            set
            {
                if (value != _textThird)
                {
                    _textThird = value;
                    OnPropertyChanged("textThird");
                }
            }
        }

        private string _symid;
        public string symid
        {
            [DebuggerStepThrough]
            get { return _symid; }
            [DebuggerStepThrough]
            set
            {
                if (value != _symid)
                {
                    _symid = value;
                    OnPropertyChanged("symid");
                }
            }
        }
        #region INotifyPropertyChanged Implementation
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string name)
        {
            var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion
    }

【问题讨论】:

    标签: c# wpf xaml windows-phone-7 windows-phone-8


    【解决方案1】:

    您的 dataReceived 集合需要这样声明,因为它是绑定的主题...

        public ObservableCollection<Data> DataReceivedCollection { get; set; } 
    

    而在初始化代码中,需要这样实例化……

            DataReceivedCollection = new ObservableCollection<Data>();
    

    你的数据类应该这样声明(不是所有的属性都声明)

    public class Data : INotifyPropertyChanged
    {
        private string _textFirst;
        public string TextFirst
        {
            [DebuggerStepThrough]
            get { return _textFirst; }
            [DebuggerStepThrough]
            set
            {
                if (value != _textFirst)
                {
                    _textFirst = value;
                    OnPropertyChanged("TextFirst");
                }
            }
        }
        private string _textSecond;
        public string TextSecond
        {
            [DebuggerStepThrough]
            get { return _textSecond; }
            [DebuggerStepThrough]
            set
            {
                if (value != _textSecond)
                {
                    _textSecond = value;
                    OnPropertyChanged("TextSecond");
                }
            }
        }
        #region INotifyPropertyChanged Implementation
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string name)
        {
            var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion
    }
    

    执行这些操作将确保绑定引擎获得填充列表框所需的信息。

    这只是一个开始,会给您带来更好的结果。正如评论中提到的,您的下一个停靠点是研究 INotifyPropertyChanged。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      • 2013-01-14
      • 2010-09-09
      • 2011-04-13
      相关资源
      最近更新 更多