[英]DataBinding to listbox
“a.text”是從我的JSON Response中提取的。 我試圖在列表框中顯示“a.text”。
List<Feature> features = App.dResult.directions[0].features;
foreach (Feature f in features)
{
Attributes a = f.attributes;
MessageBox.Show(a.text);
directionListBox.ItemsSource = a.text;
}
我嘗試將“a.text”綁定到列表框但沒有顯示。
<ListBox x:Name="directionListBox" ItemsSource="{Binding a.text}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding a.text}" Style="{StaticResource PhoneTextTitle2Style}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
任何人都可以提供一些關於如何將“a.text”綁定到列表框的想法嗎?
任何幫助將不勝感激。
ListBox的ItemsSource屬性不應指向String,如a.text,而應指向ObservableCollection。
嘗試這樣的事情:
1st:從INotifyPropertyChanged派生該代碼類。
第二:您可以從這里獲取ObservableList或使用ObservableCollection。
第3名:然后使用此代碼(未經測試,但可能有效):
ObservableList<String> featList = new ObservableCollection<String>();
public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public ObservableList<String> FeatList
{
get { return featList; }
set
{
featList = value;
InvokePropertyChanged("FeatList");
}
}
List<Feature> features = App.dResult.directions[0].features;
foreach (Feature f in features)
{
Attributes a = f.attributes;
//MessageBox.Show(a.text);
FeatList.add(a.text);
}
第四:在fild的頂部你必須使用'using'語句來導入ObservableList。
然后,將列表框的ItemSource綁定到此列表,並使TextBlock的Binding只綁定到默認的DataContext,它將是列表中的字符串:
<ListBox x:Name="directionListBox" ItemsSource="{Binding FeatList}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding}" Style="{StaticResource PhoneTextTitle2Style}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.