【问题标题】:How to do Data Binding for Textblock within a LongListselector in windows phone app?如何在 Windows Phone 应用程序的 LongListselector 中为 Textblock 进行数据绑定?
【发布时间】:2014-05-08 09:31:45
【问题描述】:

您好,我正在尝试将文本块的数据绑定到 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 phone 中以菜单格式显示的数据。 部分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;

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

【问题讨论】:

  • 它的 Json 解析 @Sajeetharan....
  • 好的,你在哪里上课?
  • 它存在于不同的命名空间下,但我能够解析和获取数据,因为我之前尝试通过 LongListselector Itemsource 绑定它。但我需要在文本块中使用此值。

标签: c# xaml windows-phone-8


【解决方案1】:

试试这样 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");
}

你的模特

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

reference

【讨论】:

    【解决方案2】:
    <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# 算法应该是:

    i) 有一个视图模型类

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多