如何访问一个XAML的DataTemplate内的控制? [英] How do I access a control inside a XAML DataTemplate?

查看:135
本文介绍了如何访问一个XAML的DataTemplate内的控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个flipview:

I have this flipview:

<FlipView x:Name="models_list" SelectionChanged="selectionChanged">
 <FlipView.ItemTemplate>
          <DataTemplate>
                <Grid x:Name="cv">
                        <Image x:Name="img1" Source = "{Binding ModelImage}" Stretch="Fill" Tag="{Binding ModelTag}"/>
                </Grid>
           </DataTemplate>
  </FlipView.ItemTemplate>

我要找到当前选定的指数IMG1。虽然寻找它,我发现了一些张贴在这里这个方法:

I want to find img1 of currently selected index. While searching for it I found this method on some post here:

private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
    {
        int childNumber = VisualTreeHelper.GetChildrenCount(control);
        for (int i = 0; i < childNumber; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(control, i);
            FrameworkElement fe = child as FrameworkElement;
            // Not a framework element or is null
            if (fe == null) return null;

            if (child is T && fe.Name== ctrlName)
            {
                // Found the control so return
                return child;
            }
            else
            {
                // Not found it - search children
                DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
                if (nextLevel != null)
                    return nextLevel;
            }
        }
        return null;
    }

它返回我的flipview的第一个索引图像,但我需要在当前选定的指标之一present ..我试图修改这个方法,但我无法找到所需的控制。谁能帮我?

It returns me the Image on the first index of flipview but I need the one present on the currently selected index.. I tried to edit this method but I am unable to find the required control. Can anyone help me?

推荐答案

您所遇到的问题是,的DataTemplate 是重复的,并正在由生成的内容的 FlipView 。在名称不被暴露,因为它会与previous兄弟已生成(或下一个将要)发生冲突。

The problem you are experiencing is that the DataTemplate is repeating and the content is being generated by the FlipView. The Name is not exposed because it would conflict with the previous sibling that was generated (or the next one that will be).

因此​​,要在一个名为元素的DataTemplate 你必须首先获取生成的项目,然后你想要的元素生成项目内进行搜索。请记住,在XAML逻辑树是如何通过名称访问的事情。生成的项目不是在逻辑树。相反,它们是在Visual树(所有的控制都是在可视化树)。这意味着它是在可视化树,你必须寻找要引用控制。在 VisualTreeHelper 可以让你做到这一点。

So, to get a named element in the DataTemplate you have to first get the generated item, and then search inside that generated item for the element you want. Remember, the Logical Tree in XAML is how you access things by name. Generated items are not in the Logical Tree. Instead, they are in the Visual Tree (all controls are in the Visual Tree). That means it is in the Visual Tree you must search for the control you want to reference. The VisualTreeHelper lets you do this.

现在,该怎么办呢?

我写了一篇关于这个的文章,因为它是这样一个反复出现的问题:<一href=\"http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html\">http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html但解决方案的肉,看起来像这样的递归方法:

I wrote an article on this because it is such a recurring question: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html but the meat of the solution is a recursive method that looks something like this:

public void TestFirstName()
{
    foreach (var item in MyFlipView.Items)
    {
        var _Container = MyFlipView.ItemContainerGenerator
            .ContainerFromItem(item);
        var _Children = AllChildren(_Container);

        var _FirstName = _Children
            // only interested in TextBoxes
            .OfType<TextBox>()
            // only interested in FirstName
            .First(x => x.Name.Equals("FirstName"));

        // test & set color
        _FirstName.Background = 
            (string.IsNullOrWhiteSpace(_FirstName.Text))
            ? new SolidColorBrush(Colors.Red)
            : new SolidColorBrush(Colors.White);
    }
}

public List<Control> AllChildren(DependencyObject parent)
{
    var _List = new List<Control>();
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        var _Child = VisualTreeHelper.GetChild(parent, i);
        if (_Child is Control)
            _List.Add(_Child as Control);
        _List.AddRange(AllChildren(_Child));
    }
    return _List;
}

这里的关键问题是,这样的方法得到所有的孩子,然后在出现的子控件列表,您可以搜索您需要的特定控制。有意义吗?

The key issue here is that a method like this gets all the children, and then in the resulting list of child controls you can search for the specific control you want. Make sense?

现在回答你的问题!

由于您特别希望当前选定的项目,你可以简单地更新code是这样的:

Because you specifically want the currently selected item, you can simply update the code like this:

if (MyFlipView.SelectedItem == null)
    return;
var _Container = MyFlipView.ItemContainerGenerator
    .ContainerFromItem(MyFlipView.SelectedItem);
// then the same as above...

这篇关于如何访问一个XAML的DataTemplate内的控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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