【问题标题】:LongListSelector not binding a array of data Windows Phone 8LongListSelector 未绑定数据数组 Windows Phone 8
【发布时间】:2014-04-04 09:11:44
【问题描述】:

我正在使用 LongListSelector 并尝试绑定一个数组。但是没有数据显示。以下是我的代码,请让我知道我做错了什么?

XAML

  <phone:LongListSelector Name="peopleLongListSelector"   
                                            ItemsSource="{Binding ComponentData}"
                                            GroupHeaderTemplate="{StaticResource LongListSelectorGroupHeaderTemmplate}"
                                            ItemTemplate="{StaticResource LongListSelectorItemTemplate}"
                                            HideEmptyGroups ="true" 
                                            IsGroupingEnabled ="true" 
                                            LayoutMode="List">
            </phone:LongListSelector>




<DataTemplate x:Key="LongListSelectorGroupHeaderTemmplate">
        <Border Background="Transparent" Padding="5">
            <Border Background="{StaticResource PhoneAccentBrush}" BorderBrush="{StaticResource PhoneAccentBrush}" BorderThickness="2"  
                                       Width="62" Height="62" Margin="0,0,18,0"                  
                                        HorizontalAlignment="Left">
                <TextBlock Text="{Binding Name}" 
                                               Foreground="{StaticResource PhoneForegroundBrush}" 
                                               FontSize="48"
                                               Padding="6"
                                               FontFamily="{StaticResource PhoneFontFamilySemiLight}"
                                               HorizontalAlignment="Left"
                                               VerticalAlignment="Center"/>
            </Border>
        </Border>
    </DataTemplate>

    <DataTemplate x:Key="LongListSelectorItemTemplate">
        <StackPanel Orientation="Horizontal" Margin="4,4">
            <Image Width="62" Height="62" Source="{Binding Image}" VerticalAlignment="Top"  Margin="0,0,15,0"/>
            <TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
        </StackPanel>
    </DataTemplate>

C#

  public class Component
    {
        public string Guid {get; set;}
        public string Name{get; set;}
        public Item[] Items{get; set;}
    }



    public class Item
        {
           public string Title{get; set;}
            public string Subtitle{get; set;}
            public string Date{get; set;}
         }

       public class MainLstViewModel : BaseViewModel
        {
             private Component[] ComponentData;
             public MainLstViewModel()
             {
               ComponentData = Proxy.GetDataAsync();
             }
        }

【问题讨论】:

  • 您正在获取异步数据 - 我猜您的 LLS 没有更新 - 您有 Binding 但没有 INotifyPropertyChanged。尝试实现该接口并运行 OnPropertyChanged()。或者也许使用 ObservableCollection 而不是 ComponentData 数组。

标签: c# wpf xaml data-binding windows-phone-8


【解决方案1】:

您的 Class 中有一些属性,并且在 xaml 中绑定不同

 <phone:LongListSelector Name="peopleLongListSelector"   
                                            ItemsSource="{Binding ComponentData}"
                                            GroupHeaderTemplate="{StaticResource LongListSelectorGroupHeaderTemmplate}"
                                            ItemTemplate="{StaticResource LongListSelectorItemTemplate}"
                                            HideEmptyGroups ="true" 
                                            IsGroupingEnabled ="true" 
                                            LayoutMode="List">
            </phone:LongListSelector>
            <DataTemplate x:Key="LongListSelectorGroupHeaderTemmplate">
            <Border Background="Transparent" Padding="5">
                <Border Background="{StaticResource PhoneAccentBrush}" BorderBrush="{StaticResource PhoneAccentBrush}" BorderThickness="2"  
                                           Width="62" Height="62" Margin="0,0,18,0"                  
                                            HorizontalAlignment="Left">
                    <TextBlock Text="{Binding Name}" 
                                                   Foreground="{StaticResource PhoneForegroundBrush}" 
                                                   FontSize="48"
                                                   Padding="6"
                                                   FontFamily="{StaticResource PhoneFontFamilySemiLight}"
                                                   HorizontalAlignment="Left"
                                                   VerticalAlignment="Center"/>
                </Border>
            </Border>
        </DataTemplate>    
        <DataTemplate x:Key="LongListSelectorItemTemplate">
            <StackPanel Orientation="Horizontal" Margin="4,4">

                <TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                <TextBlock Text="{Binding Subtitle}" Style="{StaticResource PhoneTextLargeStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                <TextBlock Text="{Binding Date}" Style="{StaticResource PhoneTextLargeStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
            </StackPanel>
        </DataTemplate>

【讨论】:

  • 在 Listview LongListSelectorGroupHeaderTemplate 我正在绑定组件类的名称属性。即使那没有被绑定......??您是否在示例应用中尝试过?
  • 你里面有东西吗
  • 是的,我有项目和多个组件。您是否尝试过此代码。这对你有用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 2014-03-12
  • 1970-01-01
  • 2017-01-27
  • 2013-07-30
相关资源
最近更新 更多