用mvvm模式实现动态的pivotitem加载

半个程序猿 2014-01-25 05:20:37
我的目的是pivotitem数量是动态确定的,写好之后发现不能工作,代码如下:
viewmodel类:
public class ViewModel
{
private ObservableCollection<TestPivot> _bindData;

public ObservableCollection<TestPivot> BindData
{
get
{
return _bindData;
}
set
{
if (value != _bindData)
{
_bindData = value;

}
}
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifiPropertyChanged(string bindData)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this,new PropertyChangedEventArgs(bindData));
}
}

public void AddData(int size)
{
BindData=new ObservableCollection<TestPivot>();
for (int i = 0; i < size; i++)
{
TestPivot t = new TestPivot();
t.Name = "piovt item" + i;
t.ListData = new List<string>();
for (int j = 0; j < 10; j++)
{
t.ListData.Add("List item" + j);
}
BindData.Add(t);
}

}
}

TestPivot类:

public class TestPivot
{
public string Name { get; set; }

public List<string> ListData { get; set; }
}

MainPage.xaml:

<phone:PhoneApplicationPage
x:Class="Demo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="DT_pivot">
<ListBox ItemsSource="{Binding ListData}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
<DataTemplate x:Key="DT_Header">
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<phone:Pivot Title="我的应用程序"
ItemsSource="{Binding BindData}"
ItemTemplate="{StaticResource DT_pivot}"
HeaderTemplate="{StaticResource DT_Header}">
</phone:Pivot>
</Grid>
</phone:PhoneApplicationPage>
...全文
695 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gnimgnot 2014-01-26
  • 打赏
  • 举报
回复
PivotItem是依赖属性吗?
半个程序猿 2014-01-25
  • 打赏
  • 举报
回复
求大神解救啊
半个程序猿 2014-01-25
  • 打赏
  • 举报
回复
ViewModel中有个INotifyPropertyChanged接口忘记继承了,但还是不行
半个程序猿 2014-01-25
  • 打赏
  • 举报
回复
今天刚接触mvvm,不是特别明白原理,请各位帮忙debug一下

7,657

社区成员

发帖
与我相关
我的任务
社区描述
Windows Phone是微软发布的一款手机操作系统,它将微软旗下的Xbox LIVE游戏、Zune音乐与独特的视频体验整合至手机中。
社区管理员
  • Windows客户端开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧