这个问题类似于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)。