基本透视项列表框数据绑定
本文关键字:数据绑定 列表 透视 | 更新日期: 2023-09-27 18:08:31
这是一个非常基本的问题,但我正试图让这个工作的问题,我所面临的是我有一个项目列表如我的mainpage.xaml
<controls:PivotItem Header="WATER">
<controls:PivotItem.Background>
<ImageBrush ImageSource="Water.png" Stretch="Uniform"/>
</controls:PivotItem.Background>
<!--Triple line list no text wrapping-->
<ListBox x:Name="SecondListBox" ItemsSource="{Binding Water}" Margin="0,0,-12,0" BorderBrush="Black" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="50">
<TextBlock Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="40" FontFamily="/Survival_Handbook;component/Fonts/Fonts.zip#XXII ARMY" Foreground="Black"/>
<!--<TextBlock Text="{Binding LineThree}" TextWrapping="NoWrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>
MainViewModelSampleData.xaml
<local:MainViewModel.Water>
<local:ItemViewModel LineOne="Finding water" LineTwo="Image" LineThree="Description"/>
<local:ItemViewModel LineOne="Purifying water" LineTwo="Image" LineThree="Description"/>
<local:ItemViewModel LineOne="Springs" LineTwo="Image" LineThree="Description"/>
<local:ItemViewModel LineOne="Digging" LineTwo="Image" LineThree="Description"/>
<local:ItemViewModel LineOne="Catchments" LineTwo="Image" LineThree="Description"/>
<local:ItemViewModel LineOne="Solar stills" LineTwo="Image" LineThree="Description"/>
<local:ItemViewModel LineOne="Sal gathering" LineTwo="Image" LineThree="Description"/>
</local:MainViewModel.Water>
MainViewModel.cs
public MainViewModel()
{
this.Water = new ObservableCollection<ItemViewModel>();
}
public ObservableCollection<ItemViewModel> Water { get; private set; }
public void LoadData()
{
this.Water.Add(new ItemViewModel() { LineOne = "Finding water", LineTwo = "Image", LineThree = "Description" });
this.Water.Add(new ItemViewModel() { LineOne = "Purifying water", LineTwo = "Image", LineThree = "Description" });
this.Water.Add(new ItemViewModel() { LineOne = "Springs", LineTwo = "Image", LineThree = "Description" });
this.Water.Add(new ItemViewModel() { LineOne = "Digging", LineTwo = "Image", LineThree = "Description" });
this.Water.Add(new ItemViewModel() { LineOne = "Catchments", LineTwo = "Image", LineThree = "Description" });
this.Water.Add(new ItemViewModel() { LineOne = "Solar stills", LineTwo = "Image", LineThree = "Description" });
this.Water.Add(new ItemViewModel() { LineOne = "Sal gathering", LineTwo = "Image", LineThree = "Description" });
}
我要做的是让每个项目从列表框中打开一个新的页面,标题为第一行,图像为第二行,其描述为第三行。我需要为每个数据透视项目创建单独的xaml吗?如果不是,那么我如何将它绑定到一个单一的xaml,它可以通过绑定的xml显示项目。我想创建的是一本书作为一个应用程序。
希望我的问题清楚易懂。
使用Tap事件代替SelectedChangedEvent更容易,bug也更少。如果您的目标是WP7.5,则无需额外的库即可工作。如果你的目标是WP7.0,那么你应该使用Silverlight工具包中的GestureListener
。
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="50"
Tap="TapEventHandler" >
<TextBlock Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}" FontSize="40" FontFamily="/Survival_Handbook;component/Fonts/Fonts.zip#XXII ARMY" Foreground="Black"/>
</StackPanel>
</DataTemplate>
然后将导航代码放在代码后面的TapEventHandler
中。在这里,您需要获得对您点击的项目的ViewModel实例的引用,计算您要导航到的位置,然后导航到新页面。
void TapEventHandler(object sender, RoutedEventArgs e)
{
ItemViewModel ivm = (ItemViewModel) sender.DataContext;
// ... find out what page/query string to navigate to based on ivm.
NavigationService.Navigate(new Uri(/*uri here*/);
}
你也可以使用MVVM框架,如MVVM Light来帮助这种管道,并消除视图和ViewModel之间的紧密依赖,但这是更高级的。