[英]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.