W.K*_*K.S 3 xaml windows-phone-8.1
我正在构建一个Windows Phone 8.1集线器应用程序.其中一个hub部分包含一个显示文章列表的ListView.我想在这个hubsection中添加一个Textblock,当文章无法下载时会显示一条消息.XAML代码如下:
<HubSection
x:Uid="ArticlesSection"
Header="ARTICLES"
DataContext="{Binding Articles}"
HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
<DataTemplate>
<Grid>
<ListView
AutomationProperties.AutomationId="ItemListViewSection3"
AutomationProperties.Name="Items In Group"
SelectionMode="None"
IsItemClickEnabled="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource BannerBackgroundArticleTemplate}"
ItemClick="ItemView_ItemClick"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
</ListView>
<TextBlock
x:Name="NoArticlesTextBlock"
HorizontalAlignment="Center"
VerticalAlignment="center"
Style="{StaticResource HeaderTextBlockStyle}"
TextWrapping="WrapWholeWords"
TextAlignment="Center"/>
</Grid>
</DataTemplate>
</HubSection>
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我无法从C#代码访问TextBlock.有更简单的方法吗?
Dec*_*oon 10
我遇到的问题是我无法从C#代码访问TextBlock.
是的,因为TextBlock是在DataTemplate中定义的,所以在应用DataTemplate之前,TextBlock将不可用.因此,x:Name属性不会在*.gics文件的InitializeComponent方法中自动生成变量引用.(有关更多信息,请阅读XAML Namescopes).
如果要从代码隐藏中访问它,有两种方法:
第一种方法是最简单的:您可以在该TextBlock的Loaded事件处理程序的sender参数中获得对TextBlock的引用.
<TextBlock Loaded="NoArticlesTextBlock_Loaded" />
Run Code Online (Sandbox Code Playgroud)
然后在你的代码隐藏中:
private TextBlock NoArticlesTextBlock;
private void NoArticlesTextBlock_Loaded(object sender, RoutedEventArgs e)
{
NoArticlesTextBlock = (TextBlock)sender;
}
Run Code Online (Sandbox Code Playgroud)
第二种方法是手动遍历可视树以找到具有所需名称的元素.这更适合动态布局,或者当您想要引用许多控件时,执行上一个方法会太乱.你可以这样做:
<Page Loaded="Page_Loaded" ... />
Run Code Online (Sandbox Code Playgroud)
然后在你的代码隐藏中:
static DependencyObject FindChildByName(DependencyObject from, string name)
{
int count = VisualTreeHelper.GetChildrenCount(from);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(from, i);
if (child is FrameworkElement && ((FrameworkElement)child).Name == name)
return child;
var result = FindChildByName(child, name);
if (result != null)
return result;
}
return null;
}
private TextBlock NoArticlesTextBlock;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
// Note: No need to start searching from the root (this), we can just start
// from the relevant HubSection or whatever. Make sure your TextBlock has
// x:Name="NoArticlesTextBlock" attribute in the XAML.
NoArticlesTextBlock = (TextBlock)FindChildByName(this, "NoArticlesTextBlock");
}
Run Code Online (Sandbox Code Playgroud)