【发布时间】:2016-10-20 18:44:42
【问题描述】:
这个问题类似于Windows Phone 8.1 Toggling the visibility of a TextBlock in a DataTemplate
还有无数其他想法,但这些想法都没有奏效。将 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)。
【问题讨论】:
标签: c# xaml data-binding win-universal-app