c# - 如何访问 XAML DataTemplate 中的控件?

标签 c# windows-8 windows-store-apps winrt-xaml flipview

我有这个翻转 View :

<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。在搜索它时,我在此处的一些帖子中找到了此方法:

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 第一个索引上的图像,但我需要当前选定索引上的图像。我尝试编辑此方法,但找不到所需的控件。谁能帮帮我?

最佳答案

您遇到的问题是 DataTemplate 重复并且内容由 FlipView 生成。 Name 未公开,因为它会与生成的前一个兄弟(或将要生成的下一个)冲突。

因此,要在 DataTemplate 中获取命名元素,您必须首先获取生成的项目,然后在生成的项目中搜索所需的元素。请记住,XAML 中的逻辑树是您按名称访问事物的方式。生成的项目不在逻辑树中。相反,它们在可视化树中(所有控件都在可视化树中)。这意味着您必须在可视化树中搜索要引用的控件。 VisualTreeHelper 让您可以做到这一点。

现在,该怎么做?

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

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

这里的关键问题是像这样的方法获取所有子控件,然后在生成的子控件列表中您可以搜索所需的特定控件。有道理吗?

现在回答你的问题!

因为你特别想要当前选中的项目,你可以像这样简单地更新代码:

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

关于c# - 如何访问 XAML DataTemplate 中的控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16375375/

相关文章:

c# - 是否真的应该为任何异步操作添加延迟?

2013-12-16T11 :20:57+0530 的 C# 日期时间格式

c# - 通过 for 循环迭代文本框

c# - 在 Mono - Linux 上使用 FileInfo 和 DirectoryInfo 类

c# - 将 MSBuild 任务中动态生成的文件添加到输出和 appx 文件夹

c# - C# 中的质因数

c# - Metro App 如何禁用 Gridview 滚动

windows-8 - d3dx11.h 不在 Windows 8.0 套件中

javascript - 将 HTML 页面从 JavaScript 转换为 PDF