在 Windows Phone 7 上将艺术家从 ArtistCollection 绑定到 PanoramaItem.Listbox
如何将 Artists
集合中的所有艺术家绑定到 PanoramaItem
中的 ListBox
?
我的xaml如下:
<controls:PanoramaItem Header="Artist" Name="Pan3">
<!--Double line list with image placeholder and text wrapping-->
<ListBox Name="artistLb" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<Rectangle Height="100" Width="100" Fill="#FFE5001b" Margin="12,0,9,0"/>
<StackPanel Width="311">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>
和xaml.cs代码:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
MediaLibrary library = new MediaLibrary();
int CountArtist = library.Artists.Count;
//binding the library.Artist to the Panorama item
}
谢谢!
How can I bind all the artists from the Artists
collection to a ListBox
in a PanoramaItem
?
My xaml is as follows:
<controls:PanoramaItem Header="Artist" Name="Pan3">
<!--Double line list with image placeholder and text wrapping-->
<ListBox Name="artistLb" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<Rectangle Height="100" Width="100" Fill="#FFE5001b" Margin="12,0,9,0"/>
<StackPanel Width="311">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>
and xaml.cs code:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
MediaLibrary library = new MediaLibrary();
int CountArtist = library.Artists.Count;
//binding the library.Artist to the Panorama item
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我的回答中,我假设您从 Windows Phone 全景项目开始,并且已经添加了对 Microsoft.Xna.Framework 的引用以获取对媒体库的访问权限。
将 ListBox 之类的 Ui 对象绑定到后台代码时,最佳解决方案是坚持使用项目中已提供的 ViewModel 方法。在您的项目中,您应该找到一个 MainViewModel。向此视图模型添加以下属性:
此属性将 MediaLibrary 公开给您的 xaml。该库在第一次调用时被实例化。
现在可以从您的 xaml 绑定到此属性,我只显示 ListBox。
请注意,我将 ListBox 绑定到我们刚刚在视图模型中创建的 Library 属性的子属性 Artists。我编辑了 ItemTemplate 以仅显示一个绑定到艺术家姓名的 TextBlock。
在模拟器上,您只会看到 1 位艺术家作为示例,要使用真实设备测试此解决方案,您必须使用 WPConnect 工具,解释如下: 此处
我希望这能让您暂时继续下去,如果仍有任何问题,请告诉我。
In my answer I will assume you started from a Windows Phone Panorama Project and already added the reference to Microsoft.Xna.Framework to gain access to the media library.
When binding Ui object like the ListBox to code behind the best solution is to stick with the ViewModel approach that is already provided in the project. In your project you should find a MainViewModel. To this viewmodel add the following property:
This property exposes the MediaLibrary to your xaml. The library is instantiated when called for the first time.
From your xaml it is now possible to bind to this property, I am only showing the ListBox.
Notice that I am binding the ListBox to the subproperty Artists of the Library property we just created in the viewmodel. I edited the ItemTemplate to show just one TextBlock that binds to the Artist name.
On your emulator you will just see 1 artist as an example, to test this solution with a real device you will have to use the WPConnect tool, which is explained here
I hope this gets you going for now, please let me know if any questions remain.
你尝试过吗?
Have you tried?