WP7 数据透视标题模板 DataContext 绑定不起作用

发布于 2024-10-26 10:34:51 字数 1887 浏览 2 评论 0原文

我有一个数据透视标题模板,以便数据透视页面具有标题和副标题。我想通过代码设置两者。

我在 Blend 中构建 XAML,无需代码绑定,它确实会显示,以便该部分正常工作。

但是,我的绑定不起作用。它要么不会构建,因为该对象没有 DataContext,要么该对象在当前上下文中不存在,或者构建但不会显示。当它不显示时,我假设我绑定到了错误的 XAML 对象。每个对象的命名只是为了让我可以找到要绑定的正确对象。

标题和副标题的绑定代码的包含类如下所示:

public class PivotTitle
{
    public string Title = "";
    public string Subtitle = "";
}

我的问题是:如何正确绑定 TitleTemplate 以便可以在代码中设置两个 TextBlock 的 Text 属性?

这是 XAML

       <controls:Pivot.TitleTemplate >
            <DataTemplate x:Name="PivotTitleTemplateDataTemplate"  >
                <StackPanel x:Name="MyPivotTitle" DataContext="{Binding}" >
                    <TextBlock x:Name="Title"
                        Text="{Binding Title}"
                        FontSize="20"
                        TextWrapping="Wrap"/>
                    <TextBlock x:Name="Subtitle"
                        Text="{Binding Subtitle}"
                        Foreground="Gray"
                        TextWrapping="Wrap"/>
                </StackPanel>
            </DataTemplate>
        </controls:Pivot.TitleTemplate>

标题和副标题取决于导航的页面。后面的代码如下所示:

//在页面类顶部定义 公共 PivotTitle _PivotTitle = new PivotTitle();

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);


        string queryStringDeploymentName = "";

        this.NavigationContext.QueryString.ContainsKey("DeploymentName"))

        _PivotTitle.Title = SelectedDeployment.DeploymentName;
        _PivotTitle.Subtitle = App.ViewModel.AppSettings.UpdatedText;
       MyPivotTitle.DataContext = _PivotTitle;
     }

此特定示例不会生成此错误:名称“MyPivotTitle”在当前上下文中不存在。数据透视表页面的列表框上的绑定工作正常。

我相信我的代码隐藏和整体 XAML 是正确的。我认为我的 DataContext 绑定不正确。

感谢您的任何帮助。

I have a Pivot Title Template so that the Pivot page has a title and subtitle. I want to set both via code.

I build the XAML in Blend and without code binding, it does display so that part works.

However, my binding isn't working. It either won't build because the object doesn't have a DataContext, or the object doens't exist in current context or does build but won't display. When it doesn't display, I assume I'm binding to the wrong XAML object.Each object is named only so I could find the right object to bind to.

The containing class for the bound code of Title and Subtitle looks like:

public class PivotTitle
{
    public string Title = "";
    public string Subtitle = "";
}

My question is: how do I correctly bind the TitleTemplate so that the two TextBlocks' Text properties can be set in code?

Here is the XAML

       <controls:Pivot.TitleTemplate >
            <DataTemplate x:Name="PivotTitleTemplateDataTemplate"  >
                <StackPanel x:Name="MyPivotTitle" DataContext="{Binding}" >
                    <TextBlock x:Name="Title"
                        Text="{Binding Title}"
                        FontSize="20"
                        TextWrapping="Wrap"/>
                    <TextBlock x:Name="Subtitle"
                        Text="{Binding Subtitle}"
                        Foreground="Gray"
                        TextWrapping="Wrap"/>
                </StackPanel>
            </DataTemplate>
        </controls:Pivot.TitleTemplate>

The title and subtitle are dependent on the page navigated from. The code behind looks like:

//defined at top of page class
public PivotTitle _PivotTitle = new PivotTitle();

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);


        string queryStringDeploymentName = "";

        this.NavigationContext.QueryString.ContainsKey("DeploymentName"))

        _PivotTitle.Title = SelectedDeployment.DeploymentName;
        _PivotTitle.Subtitle = App.ViewModel.AppSettings.UpdatedText;
       MyPivotTitle.DataContext = _PivotTitle;
     }

This particular example won't build with this error: The name 'MyPivotTitle' does not exist in the current context. The binding on the Pivot page's Listbox is working correctly.

I believe my codebehind and overall XAML are correct. I think I'm DataContext binding incorrectly.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

活雷疯 2024-11-02 10:34:51

在您的示例中, MyPivotTitle 是 DataTemplate 中的一个元素,因此无法直接从您的代码访问,因此会出现错误。如果您需要访问项目控件模板内的控件(例如 Pivot 或 ListBox`),那么您可能会发现 WIndowsPhoneGeek.com 的这篇文章很有用。

然而,听起来你也许可以在没有任何“黑客攻击”的情况下做到这一点。此标题和副标题仅适用于单个枢轴项目,还是适用于所有枢轴项目?您还在数据透视项上绑定了哪些其他数据(如果有)?

In your example MyPivotTitle is an element within the DataTemplate, so cannot be accessed directly from your code, hence the error. If you need to access a control inside a template of an items control (such as Pivot or ListBox`) then you may find this article from WIndowsPhoneGeek.com useful.

However, it sounds like you might be able to do this without any "hacking". Is this title and subtitle just for a single pivot item, or will it be on all of them? What other data, if any are you binding on the pivot item(s)?

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