【问题标题】:wp8 bind every PivotItem to different Listwp8 将每个 PivotItem 绑定到不同的 List
【发布时间】:2015-12-22 04:26:00
【问题描述】:

我已经使用 Pivot 控件创建了 WP8 应用程序。有 5 个 PivotItems,其中 3 个应绑定到 3 个列表或一个具有不同过滤数据的列表。如何做到这一点?

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 中获得了正确的数据,并且没有显示加载数据,这不是问题。 我不知道如何将 TextBlock 从 LongListSelector(Text=some binding)绑定到属性 ssubname,这是 clsPivotItemList 类的属性,而 List 是 clsPivotData 类的属性。

【问题讨论】:

    标签: c# windows-phone-8 data-binding pivotitem


    【解决方案1】:

    我终于找到了答案。我将下一个代码放在 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 不在 MainVewModel 中,而是在 MainPage 中,并且 MainPage 的 DataContext 被设置为 MainViewModel ViewModel 对象。 这个新的解决方案给了我预期的结果,这意味着 ListBox 已被填充。

    【讨论】:

      【解决方案2】:
      <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>
      

      【讨论】:

      • 我得到了空白页,它接近解决方案,但我应该做一些更正。我需要在列表中绑定属性,这是主类的属性。无论如何谢谢你。
      【解决方案3】:

      试试这个&lt;TextBlock Text="{Binding ssubname}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource TextBlockMnuTekst}"/&gt;

      【讨论】:

        猜你喜欢
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 2013-12-07
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多