c# - Databound pivot 未在 Windows Phone 8 中加载第一个 PivotItem

标签 c# xaml windows-phone-8

我正在将我的应用程序从 WP7 迁移到 WP8,一件有趣的事情发生了。我有一个在 WP7 中完美运行的数据绑定(bind)枢纽。但是,在 WP8 中,完全相同的代码不会加载第一个 PivotItem。我已经尝试了 question for WP7 中的所有解决方案,都不起作用(我想要一个解决方案,而不是一个丑陋的解决方法)。我在构造函数中设置 DataContext,集合正常,一切正常。当我在应用程序中滚动时,它只会加载枢轴。谁有解决办法?

最佳答案

我无法在 WP8 上重现 Pivot 的任何数据绑定(bind)问题。 WP8 上的全景数据绑定(bind)存在一个已知问题,但不是 Pivot。什么对你不起作用?

这是一个基本的 WP8 Pivot 数据绑定(bind)代码,对我来说工作得很好。

C# 代码将 DataContext 设置为奶牛的可观察集合:

this.DataContext = new ObservableCollection<Cow>()
                           {
                               new Cow("Foo"),
                               new Cow("Bar"),
                               new Cow("Baz")
                           };

public class Cow
{
    public Cow(string name)
    {
        Name = name;
    }

    public string Name { get; set; }
}

XAML 代码使用该 DataContext 作为 ItemSource 并将 PivotItem.Header 和 PivotItem.Content 绑定(bind)到奶牛名称。

<phone:Pivot ItemsSource="{Binding}">
    <phone:Pivot.HeaderTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding Name}" />
        </DataTemplate>
    </phone:Pivot.HeaderTemplate>
    <phone:Pivot.ItemTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding Name}" />
        </DataTemplate>
    </phone:Pivot.ItemTemplate>
</phone:Pivot>

工作得很好......

Pivot with Foo active Pivot with Bar active

关于c# - Databound pivot 未在 Windows Phone 8 中加载第一个 PivotItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023948/

相关文章:

c# - WPF 绑定(bind)路径=/不工作?

cordova - 如何在 windows phone 中使用 phonegap sqlite 插件

c# - 无法使用正则表达式与 "?"匹配字符串

c# - MSBuild:如果给定绝对路径,如何获取文件的相对路径?

c# - WPF 行为 + Storyboard动画 = AssociatedObject 为 null

c# - WPF 数据网格 : How to perform column binding using code behind?

c# - "Symbols for the module MyLibrary.dll were not loaded"?

c# - 从代码启动音乐应用程序

c# - VS 2017 .Net Core 2.0 控制台应用程序发布失败

c# - 打开 URL 的自定义图标