2015-09-24 51 views
0

我已經使用Pivot controll創建了WP8應用程序。有5個PivotItems,其中3個應該被綁定到3個列表或一個帶有不同過濾數據的列表。這個怎麼做?wp8將每個PivotItem綁定到不同的列表

XAML:

<phone:Pivot Name="PControl" Grid.Row="1" Style="{StaticResource PivotStyle}"> 
 
       <phone:PivotItem> 
 
        <phone:PivotItem.Header> 
 
         <Grid Height="80"> 
 
          <TextBlock Text="offer" FontSize="45" Height="80" /> 
 
         </Grid> 
 
        </phone:PivotItem.Header> 
 
        <phone:LongListSelector Margin="0,0,0,0" ItemsSource="{Binding lstOffer}"> 
 
         <phone:LongListSelector.ItemTemplate> 
 
          <DataTemplate> 
 
           <StackPanel Margin="0,0,0,17"> 
 
            <TextBlock Text="some binding" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource TextBlockMnuTekst}"/> 
 
           </StackPanel> 
 
          </DataTemplate> 
 
         </phone:LongListSelector.ItemTemplate> 
 
        </phone:LongListSelector> 
 
       </phone:PivotItem> 
 
    </phone:Pivot> 
 
. 
 
. 
 
. 
 

 
<phone:PivotItem> 
 
        <phone:PivotItem.Header> 
 
         <Grid Height="80"> 
 
          <TextBlock Text="services" FontSize="45" Height="80" /> 
 
         </Grid> 
 
        </phone:PivotItem.Header> 
 
        <phone:LongListSelector Margin="0,0,0,0" ItemsSource="{Binding lstServices}"> 
 
         <phone:LongListSelector.ItemTemplate> 
 
          <DataTemplate> 
 
           <StackPanel Margin="0,0,0,17"> 
 
            <TextBlock Text="some binding" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource TextBlockMnuTekst}"/> 
 
           </StackPanel> 
 
          </DataTemplate> 
 
         </phone:LongListSelector.ItemTemplate> 
 
        </phone:LongListSelector> 
 
       </phone:PivotItem>

C#:

public class clsPivotData 
 
    { 
 
     public string Name { get; set; } 
 
     public List<clsPivotItemList> PivotItemList { get; set; } 
 

 
     public clsPivotData() 
 
     { 
 
      PivotItemList = new List<clsPivotItemList>(); 
 
     } 
 
    } 
 

 
    public class clsPivotItemList 
 
    { 
 
     public string id { get; set; } 
 
     public string ssubname { get; set; } 
 
     public string visible { get; set; } 
 
     public string icon { get; set; } 
 
     public string itemType { get; set; } 
 
     public string url { get; set; } 
 
     public string desc { get; set; } 
 

 
    } 
 
    
 
    public partial class MainPage : PhoneApplicationPage 
 
    { 
 
     public ObservableCollection<clsPivotData> Items { get; set; } 
 
     public static MainPage CurrentMainPage; 
 

 
     public List<clsPivotData> lstOffer { get; set; } 
 
     public List<clsPivotData> lstServices { get; set; } 
 
     public List<clsPivotData> lstInfo { get; set; } 
 

 
     // Constructor 
 
     public MainPage() 
 
     { 
 
      InitializeComponent(); 
 

 
      CurrentMainPage = this; 
 

 
      // Set the data context of the listbox control to the sample data 
 
      DataContext = App.ViewModel; 
 

 
      lstOffer = new List<clsPivotData>(from i in Items where i.Name == "offer" select i); 
 
      lstServices = new List<clsPivotData>(from i in Items where i.Name == "services" select i); 
 
      lstInfo = new List<clsPivotData>(from i in Items where i.Name == "info" select i); 
 
. 
 
. 
 
.

我得到正確的數據在lstOffer和lstServices並沒有SH ow加載數據,那不是問題。 我不知道如何將TextBlock從LongListSelector(Text =一些綁定)綁定到屬性ssubname,它是clsPivotItemList類的屬性,List是clsPivotData類的屬性。

回答

0

我終於找到了答案。我把下面的代碼在MainViewModel類:

public ObservableCollection<clsPivotData> Items { get; set; } 
 
     
 
public ObservableCollection<clsPivotData> lstOffer { get; set; } 
 
public ObservableCollection<clsPivotData> lstServices { get; set; } 
 
public ObservableCollection<clsPivotData> lstInfo { get; set; } 
 

 
// Constructor 
 
    public MainViewModel() 
 
    { 
 
    this.Items = new ObservableCollection<clsPivotData>(); 
 

 
    LoadData(); 
 
    lstOffer = Items.Where(o => o.Name == "offer").FirstOrDefault().PivotItems; 
 
    lstServices = Items.Where(o => o.Name == "service").FirstOrDefault().PivotItems; 
 
    lstInfo = Items.Where(o => o.Name == "info").FirstOrDefault().PivotItems; }

在XAML我插在這樣的ListBox:

<ListBox Margin="0,0,0,0" ItemsSource="{Binding lstOffer}"> 
 
    <ListBox.ItemTemplate> 
 
     <DataTemplate> 
 
      <ItemsControl> 
 
        <StackPanel Margin="0,0,0,17"> 
 
         <TextBlock Text="{Binding ssubname}" TextWrapping="Wrap" Margin="12,0,12,0" Style="{StaticResource TextBlockMnuTekst30}"/> 
 
        </StackPanel> 
 
       </ItemsControl> 
 
     </DataTemplate> 
 
    </ListBox.ItemTemplate> 
 
    </ListBox> 
 

 
. 
 
. 
 
.
在以前的解決方案,lstOffer,lstServices和lstInfo WASN在MainVewModel中,但在MainPage和MainPage的DataContext中被設置爲MainViewModel ViewModel對象。 這個新的解決方案給了我期望的結果,這意味着ListBox被填充。

0

試試這個<TextBlock Text="{Binding ssubname}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource TextBlockMnuTekst}"/>

0
<Grid Datacontext="{Binding lstOffer }"> 
<phone:LongListSelector Margin="0,0,0,0" ItemsSource="{Binding PivotItemList }"> 
         <phone:LongListSelector.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Margin="0,0,0,17"> 
            <TextBlock Text="{Binding ssubname}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource TextBlockMnuTekst}"/> 
           </StackPanel> 
          </DataTemplate> 
         </phone:LongListSelector.ItemTemplate> 
        </phone:LongListSelector> 
</Grid> 
+0

我得到空的頁面,它接近解決方案,但我應該做一些更正。我需要綁定列表中的屬性,這是主類的屬性。不管怎樣,謝謝你。 – dragy

相關問題