自动将数据绑定到grid.textblock

时间:2014-04-26 22:08:02

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

我有问题,我不知道如何绑定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
    }

1 个答案:

答案 0 :(得分:3)

您的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的研究。