简体   繁体   中英

Access xaml control inside ith item in 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>

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

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

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

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. I've tried this code but the line

var myTextBlock= _Children.OfType<TextBlock>().FirstOrDefault(c => c.Name.Equals("test")); specifically

.OfType<TextBlock>() gives an error

'System.Collections.Generic.List<Windows.UI.Xaml.Controls.TextBlock>' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of type 'System.Collections.Generic.List<Windows.UI.Xaml.Controls.TextBlock>' could be found (are you missing a using directive or an assembly reference?)


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

I also get a warning for the line

var item = itemsControl.ItemContainerGenerator.ContainerFromItem(o); Windows.UI.Xaml.Controls.ItemContainerGenerator.ContainerFromItem(o); is obsolote.'ContainerForm' may be unavailable for releases after Windows Phone 8.1. Use itemsControl.ContainerFromItem instead.

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

Please Help


UPDATE:

I'm using the following

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";
     }
 } 
}

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;

}

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.

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

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.
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM