2015-10-20 102 views
0

这个问题类似于Windows Phone 8.1 Toggling the visibility of a TextBlock in a DataTemplate文本装订到文本框在HubSection

和无数人,但没有这些想法都在工作。将我的Textblock添加到我的集线器中的数据模板后,永不会触发Loaded事件。 Visual Tree搜索没有找到我的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)。

回答

0

我是想分配给在的OnNavigatedTo方法的文本块这是正文块的加载方法之前调用。这就是为什么我的程序显示textblock为空。

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