如何在Windows Phone应用程序的LongListselector中为Textblock进行数据绑定?

时间:2014-05-08 09:31:45

标签: c# xaml windows-phone-8

嗨,我正在尝试在LongListSelector内绑定文本块的数据。但我没有得到任何输出,请帮助我。

这是我的XAML代码:

<phone:LongListSelector ItemsSource="{Binding ''}"  x:Name="longListSelector" HorizontalAlignment="Left" Height="680" VerticalAlignment="Top" Width="446"  >
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                <TextBlock Name="name" Text="{Binding DataContext.TextContent,ElementName=page,Mode=OneWay}" Height="100" Width="100" HorizontalAlignment="Center">

                        </TextBlock>
                        </StackPanel>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>

在C#代码中,我以菜单格式解析了需要在Windows手机中显示的数据。 C#代码的一部分如下所示:

 XDocument document = XDocument.Parse(e.Result);
            var data1 = from query in document.Descendants("location")
                        select new Data
                        {
                            Lat = (string)query.Element("lat"),
                            Lag = (string)query.Element("lng")

                        };
            foreach (var d in data1)
            {
                JsonParsing(d.Lat, d.Lag);
            }
            data1 = from query in document.Descendants("result")
                    select new Data
                    {
                        Country = (string)query.Element("formatted_address")
                    };
            foreach (var d in data1)
            {
               // ob.JsonParsing(d.Lat, d.Lag);
                //XmlParsing(d.Lat, d.Lag);
                val = d.Country;
                //listbox.Items.Add(val);
                //StringsList.Add(val);

                 TextContent=val;

我希望国家/地区的价值显示在文本块中,请帮我解决这个问题,因为我对这个领域很陌生,谢谢。

2 个答案:

答案 0 :(得分:0)

试试这样 a good reference     

 <DataTemplate>
  <StackPanel VerticalAlignment="Top">
     <TextBlock Text="{Binding Value}" />
  </StackPanel>

</LongListSelector>

代码隐藏

 **Add a public property only public property can be participate in databinding**

   #region Public Properties


  private ObservableCollection<YourModel> _collectionofValue;

    public ObservableCollection<YourModel> CollectionofValues
    {
        get
        {

            return _collectionofValue;
        }
        set
        {
            _collectionofValue=value;
            raisepropertyChanged("CollectionofValues");
        }
    }

   private string _value;

    public string Value
    {
        get
        {
            return _errorMessage;
        }
        set
        {
            _errorMessage = value;
            RaisePropertyChanged("Value");
        }
    }
    #endregion

   **Set value to this public property when you get value**


// for single values
public void getValue()
{
  value =GetXmlValue(); // your method that will return the value;
}

//  as it is a collection 
 public void getValuestoCollection()
{
   Collection.Add(new YourModel(value="SampleValue1");
 Collection.Add(new YourModel(value="SampleValue1");
 Collection.Add(new YourModel(value="SampleValue1");
  Collection.Add(new YourModel(value="SampleValue1");
}

YourModel

   // the collection of this model is binded to the LongListSelector.
    public class ModelName
    {
       public string Values {get;set;}
    }

reference

答案 1 :(得分:0)

<phone:LongListSelector ItemsSource="{Binding Items}"  x:Name="longListSelector" HorizontalAlignment="Left" Height="680" VerticalAlignment="Top" Width="446"  >
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                <TextBlock Name="name" Text="{Binding Path=TextContent}" Height="100" Width="100" HorizontalAlignment="Center">

                        </TextBlock>
                        </StackPanel>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>

你的C#algm应该是:

i)有一个viewmodel类

public class MyViewModel
{
public ObservableCollection<MyDataItem> Items {get; set;}

public MyViewModel()
{
Items=new ObservableCollection<MyDataItem>();

loop //add your items to your 'Items' property so that you can bind this with LongListSelector ItemsSource
{
Items.Add(new MyDataItem("mystring"));
}

}



}

public class MyDataItem 
{
public MyDataItem(string s)
{
TextContent=s;
}

public string TextContent {get;set;}
}

ii)为ViewModel类创建一个实例并设置DataContext //在包含LongListSelector

的页面的构造函数中写这个
public MyViewModel vm;

constructor()
{
vm=new MyViewModel();
this.DataContext=vm;
}