简体   繁体   中英

Binding Text to Textbox in HubSection

This question is similar to Windows Phone 8.1 Toggling the visibility of a TextBlock in a DataTemplate

and countless others, but none of these ideas are working. The Loaded event is never triggered after I add my Textblock to my datatemplate in my hub. The Visual Tree search is not finding my TextBlock.

I have tried a basic binding like this:

            <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>

with

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

But this doesn't work (Text on the Hub is always empty). From looking at previous posts and code I have come up with this xaml code using Dependency Properties:

             <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>

with this in the 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); }
    }

and I set the text with

DesiredSelectionText = makeText.Text;

The width binding works perfectly, but the text is not updating. What is the proper way to change Hub/DataTemplate Text at Runtime? Since the Hub is not even printing "Nothing", something must be really wrong.

As a last resort I am thinking I will just construct my own datatemplate and assign it at runtime, but the only code I can find for that is deprecated(uses FrameworkElementFactory).

I was trying to assign to the textblock in the OnNavigatedTo method which is called before the textblock's Loaded method. That's why my program was showing the textblock as null.

Turns out the link I posted is the solution, just for me, the textblock was loaded after the page was navigated to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM