C# 自动将数据绑定到grid.textblock
我的问题是,我不知道如何在windows phone 8中绑定数据,情况是:- 我从服务器请求数据,得到响应后,我将它们显示在网格和列表框中(如表格)。C# 自动将数据绑定到grid.textblock,c#,wpf,xaml,windows-phone-7,windows-phone-8,C#,Wpf,Xaml,Windows Phone 7,Windows Phone 8,我的问题是,我不知道如何在windows phone 8中绑定数据,情况是:- 我从服务器请求数据,得到响应后,我将它们显示在网格和列表框中(如表格)。 在这里之前,在我完成display函数之后,一切都很好,有一个新函数用于检索最后一个请求的更新 这是我的问题: 当我收到新数据时,我不知道如何将新数据绑定到同一个旧表。 例如:第一个请求是返回黄金价格1500$-->我在表中显示--->然后是新请求-->更新函数返回黄金的新价格为1502$。 如何在应用程序运行时用新价格更新具有gold pri
在这里之前,在我完成display函数之后,一切都很好,有一个新函数用于检索最后一个请求的更新 这是我的问题:
当我收到新数据时,我不知道如何将新数据绑定到同一个旧表。
例如:第一个请求是返回黄金价格1500$-->我在表中显示--->然后是新请求-->更新函数返回黄金的新价格为1502$。
如何在应用程序运行时用新价格更新具有gold price textblock的所需行 这是第一个请求:-
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);
}
您的dataReceived集合需要这样声明,因为它是绑定的主题
public ObservableCollection<Data> DataReceivedCollection { get; set; }
执行这些操作将确保绑定引擎获得填充列表框所需的信息
这只是一个开始,会给你一些更好的结果。正如评论中提到的,您的下一个停靠港是研究INotifyPropertyChanged
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
}
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
}