在第i个flipview项目里访问控制XAML [英] Access xaml control inside ith item in flipview

查看:124
本文介绍了在第i个flipview项目里访问控制XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由一些代码填充一个flipview(我不知道如何修改应用程序)。

I have a flipview which is populated by some code (I don't understand how modifying an app).

<FlipView  x:Name="ArticleDetail" Grid.Row="1" AutomationProperties.AutomationId="ItemsFlipView" AutomationProperties.Name="Item Details" TabIndex="1"
             DataContext="{Binding LatestArticlesModel}"
             ItemsSource="{Binding Items}"
             ItemTemplate="{StaticResource LatestArticles1DetailDetail}"
             SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
             ItemContainerStyle="{StaticResource FlipItemStyle}">
</FlipView>

<!--Data template for flipview-->
<DataTemplate x:Key="LatestArticles1DetailDetail">
      <ScrollViewer>
        <StackPanel>
             <TextBlock Margin="0,16" 
                  Text="{Binding Title, Converter={StaticResource    TextPlainConverter}, ConverterParameter = 140}" 
                  Style="{StaticResource SubHeaderText}" />
              <Image Source="{Binding ImageUrl, Converter={StaticResource ThumbnailConverter},      ConverterParameter=300}" Stretch="UniformToFill" />
              <TextBlock 
                   x:Name="FeedUrl" 
                   Margin="0,12"  Style="{StaticResource Html2XamlStyleText}"
                   Text="{Binding FeedUrl}" 
                   Visibility="Collapsed"/>
              <RichTextBlock 
                    x:Name="Content" 
                    Margin="0,12"  
                    Style="{StaticResource Html2XamlStyle}"/>
            </StackPanel>
        </ScrollViewer>
</DataTemplate>

这是一个名为FeedUrl我想提取存储在它的URL文本块。

From the textblock named "FeedUrl" I want to extract the url which is stored in it.

使用的URL来解析HTML页面指向的网址

Use the url to parse the html page pointed to by that url

处理显示在richtextblock一些内容以后命名内容。

After processing display some content in the richtextblock named "content".

我现在面临的是如何得到参考flipview每个项目内的文本块和richtextblock唯一的问题。

The only problem I am facing in it is how to get reference to the textblock and richtextblock inside each item of the flipview.

有关获取参考我已经尝试了两个解决方案项目:

For getting reference to items I have tried two solutions:


  1. 我已经试过这代码而行

  1. I've tried this code but the line

VAR myTextBlock = _Children.OfType< TextBlock的>()FirstOrDefault(C =方式> c.Name .Equals(测试)); 专门

.OfType< TextBlock的>()给出了一个错误

'System.Collections.Generic.List< Windows.UI.Xaml.Controls.TextBlock>'不包含定义OfType',没有扩展方法'OfType接受式的第一个参数System.Collections.Generic.List< Windows.UI.Xaml.Controls.TextBlock>'可以找到(是否缺少using指令或程序集引用?)


  1. 我还试图给出另一种解决方案的这里,但我总是得到一个空引用。

  1. I also tried another solution given here, but I always get a null reference.

我也得到了该行警告

VAR项目= itemsControl.ItemContainerGenerator.ContainerFromItem(O);
Windows.UI.Xaml.Controls.ItemContainerGenerator.ContainerFromItem(O);是obsolote.'ContainerForm可能是Windows Phone的8.1版本后无法使用。使用itemsControl.ContainerFromItem代替。

即使我使用 itemsControl.ContainerFromItem 它总是。返回空引用

Even if I use itemsControl.ContainerFromItem it always returns a null reference.

请帮助

更新:

我用下面的

if(!statusiop.statusup){
this.UpdateLayout();
for (int i = 0; i < ArticleDetail.Items.Count; i++)
{
var fvItem = this.ArticleDetail.Items[i];
var container = this.ArticleDetail.ContainerFromItem(fvItem);
 if (container == null)
 {
     Text = "null container";
 }
 else
 {
     var tbFeedURL = FindElementByName<TextBlock>(container, "FeedUrl");
     if (tbFeedURL == null)
     {
         test.Text = "null text";
     }
     else
     {
         tbFeedURL.Text = tbFeedURL.Text + "Test";
     }
 } 
}



我经历了所有的项目迭代在flipview,并根据需要修改数据。我还使用一个公共静态类

I iterate through all the items in the flipview, and modify the data as needed. I am also using a public static class

public static class statusiop
{
    public static Boolean statusup= false;

}



其中包含一个成员statusup。 statusup作为一个标志,这表明flipview数据已更新一次,而无需再进行更新时,正确的。

which contains a member statusup. statusup serves as a flag which when true indicates that the flipview data has been updated once and need not be updated again.

推荐答案

您需要VisualTreeHelper方法。这仅仅是一些代码,我使用。我想你可以很容易地调整到您的需要。

You need a VisualTreeHelper method. This is just some code I am using. I think you can easily adjust it to your needs.

先放FindElementByName方法的地方到你的代码隐藏文件:

First put the FindElementByName method somewhere into your code behind file:

public T FindElementByName<T>(DependencyObject element, string sChildName) where T : FrameworkElement
    {
        T childElement = null;
        var nChildCount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < nChildCount; i++)
        {
            FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

            if (child == null)
                continue;

            if (child is T && child.Name.Equals(sChildName))
            {
                childElement = (T)child;
                break;
            }

            childElement = FindElementByName<T>(child, sChildName);

            if (childElement != null)
                break;
        }
        return childElement;
    }

现在你可以开始使用方法:

Now you can start using the method:

this.UpdateLayout();
var fvItem = this.ArticleDetail.Items[ArticleDetail.SelectedIndex];
var container = this.ArticleDetail.ContainerFromItem(fvItem);
// NPE safety, deny first
if (container == null)
    return;
var tbFeedURL = FindElementByName<TextBlock>(container, "FeedUrl");
// And again deny if we got null
if (tbFeedURL == null)
    return;
/*
  Start doing your stuff here.
*/

这篇关于在第i个flipview项目里访问控制XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆