将文本绑定到 HubSection 中的文本框

Binding Text to Textbox in HubSection

这个问题类似于Windows Phone 8.1 Toggling the visibility of a TextBlock in a DataTemplate

和无数其他想法,但这些想法中有 none 行之有效。在我将 Textblock 添加到中心的数据模板后,从未触发 Loaded 事件。视觉树搜索未找到我的 TextBlock。

我试过这样的基本绑定:

            <HubSection Background="{StaticResource HubSectionBackgroundBrush}"
                        MaxWidth="{x:Bind DesiredHubSectionWidth, Mode=OneWay}"
                        Header="You have selected:" Padding="60" 
                        >
                <DataTemplate x:DataType="local:Scenario4">
                    <TextBlock TextWrapping="Wrap" Style="{StaticResource BasicTextStyle}" HorizontalAlignment="Left" Text="{Binding Item}"/>
                </DataTemplate> 
            </HubSection>

public string Item { get; set; }
Item = makeText.Text;

但这不起作用(集线器上的文本始终为空)。通过查看以前的帖子和代码,我想出了这个 xaml 使用依赖属性的代码:

             <HubSection Background="{StaticResource HubSectionBackgroundBrush}"
                        MaxWidth="{x:Bind DesiredHubSectionWidth, Mode=OneWay}"
                        Header="You have selected:" Padding="60"
                        >
                <DataTemplate x:DataType="local:Scenario4">
                    <TextBlock TextWrapping="Wrap" Style="{StaticResource BasicTextStyle}" HorizontalAlignment="Left" Text="{x:Bind DesiredSelectionText, Mode=OneWay}"/>
                </DataTemplate> 
            </HubSection>

在 c# 中使用这个

    private static DependencyProperty s_desiredHubSectionWidthProperty
        = DependencyProperty.Register("DesiredHubSectionWidth", typeof(double), typeof(Scenario4), new PropertyMetadata(560.0));

    private static DependencyProperty selectionText = DependencyProperty.Register("SelectionText", typeof(string), typeof(Scenario4), new PropertyMetadata("Nothing"));

    public static DependencyProperty DesiredHubSectionWidthProperty
    {
        get { return s_desiredHubSectionWidthProperty; }
    }

    public static DependencyProperty DesiredSelectionTextProperty
    {
        get { return selectionText; }
    }

    public string DesiredSelectionText
    {
        get { return (string)GetValue(selectionText); }
        set { SetValue(selectionText, value); }
    }

    public double DesiredHubSectionWidth
    {
        get { return (double)GetValue(s_desiredHubSectionWidthProperty); }
        set { SetValue(s_desiredHubSectionWidthProperty, value); }
    }

我用

设置文本
DesiredSelectionText = makeText.Text;

宽度绑定工作正常,但文本没有更新。在运行时更改 Hub/DataTemplate 文本的正确方法是什么?由于 Hub 甚至没有打印 "Nothing",一定是出了什么问题。

作为最后的手段,我想我将构建自己的数据模板并在运行时分配它,但我能找到的唯一代码已被弃用(使用 FrameworkElementFactory)。

我试图在 OnNavigatedTo 方法中分配给文本块,该方法在文本块的 Loaded 方法之前调用。这就是为什么我的程序将文本块显示为空。

原来我发布的 link 是解决方案,只是对我来说,在页面导航到后加载了文本块。