Access xaml control inside ith item in flipview

前端 未结 1 1395
忘掉有多难
忘掉有多难 2020-12-22 01:29

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



        
相关标签:
1条回答
  • 2020-12-22 02:07

    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.
    */
    
    0 讨论(0)
提交回复
热议问题