简体   繁体   English

DataBinding到列表框

[英]DataBinding to listbox

The "a.text" is extracted from my JSON Response. “a.text”是从我的JSON Response中提取的。 I am trying to display the "a.text" in the listbox. 我试图在列表框中显示“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;

        }

I tried using binding the "a.text" to the listbox but there is no display. 我尝试将“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>

Can anyone provide me with some idea on how to bind the "a.text" to listbox? 任何人都可以提供一些关于如何将“a.text”绑定到列表框的想法吗?

Any help will be greatly appreciated. 任何帮助将不胜感激。

The ItemsSource Property of a ListBox should not point to a String, like a.text, but to an ObservableCollection. ListBox的ItemsSource属性不应指向String,如a.text,而应指向ObservableCollection。

Try something like this: 尝试这样的事情:

1st: derive that code class from INotifyPropertyChanged. 1st:从INotifyPropertyChanged派生该代码类。

2nd: You can get the ObservableList from here or use ObservableCollection instead. 第二:您可以从这里获取ObservableList或使用ObservableCollection。

3rd: Then use this code (untested, but might work): 第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);
}

4th: At the top of the fild you have to use a 'using' statement to import the ObservableList. 第四:在fild的顶部你必须使用'using'语句来导入ObservableList。

Then, bind the listbox'es ItemSource to this list, and make the TextBlock's Binding just bind to the default DataContext, which will be the string from the list: 然后,将列表框的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM