在 Windows Phone 7 上将艺术家从 ArtistCollection 绑定到 PanoramaItem.Listbox

发布于 2024-11-28 02:10:59 字数 1568 浏览 4 评论 0原文

如何将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤寂小茶 2024-12-05 02:10:59

在我的回答中,我假设您从 Windows Phone 全景项目开始,并且已经添加了对 Microsoft.Xna.Framework 的引用以获取对媒体库的访问权限。

将 ListBox 之类的 Ui 对象绑定到后台代码时,最佳解决方案是坚持使用项目中已提供的 ViewModel 方法。在您的项目中,您应该找到一个 MainViewModel。向此视图模型添加以下属性:

    private MediaLibrary _library;
    public MediaLibrary Library
    {
        get
        {
            if (_library == null)
            {
                _library = new MediaLibrary();
            }
            return _library;
        }
    }

此属性将 MediaLibrary 公开给您的 xaml。该库在第一次调用时被实例化。

现在可以从您的 xaml 绑定到此属性,我只显示 ListBox。

            <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Library.Artists}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                            <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>

                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </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:

    private MediaLibrary _library;
    public MediaLibrary Library
    {
        get
        {
            if (_library == null)
            {
                _library = new MediaLibrary();
            }
            return _library;
        }
    }

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.

            <ListBox Margin="0,0,-12,0" ItemsSource="{Binding Library.Artists}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                            <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>

                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </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.

橙幽之幻 2024-12-05 02:10:59

你尝试过吗?

artistLb.DataContext = library.Artists;

Have you tried?

artistLb.DataContext = library.Artists;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文