简体   繁体   English

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

[英]How to do Data Binding for Textblock within a LongListselector in windows phone app?

Hi, I am trying to bind the data for text block within a LongListSelector . 嗨,我正在尝试将数据绑定到LongListSelector内的文本块。 But I am not getting any Output for it, kindly help me. 但是我没有得到任何输出,请帮助我。

This is my XAML code: 这是我的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>

In the C# code I have parsed data which i need to display in the windows phone, in a menu format. 在C#代码中,我解析了我需要以菜单格式显示在Windows Phone中的数据。 Part of C# code is shown below: 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;

I want the value of the country to be shown inside the textblock, kindly help me figure this out as I am pretty new to this field, thanks. 我希望在文本框内显示该国家的价值,请帮我解决这个问题,因为我对此领域还很陌生。

try like this a good reference 像这样尝试一个很好的参考

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

</LongListSelector>

CodeBehind 代码背后

 **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 参考

<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>

Your C# algm should be: 您的C#algm应该是:

i) Have a viewmodel class 我)有一个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) Create an instance to ViewModel class and set DataContext // write this in the constructor of the page which contains the LongListSelector ii)创建ViewModel类的实例并设置DataContext //将其写入包含LongListSelector的页面的构造函数中

public MyViewModel vm;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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