在 WPF itemscontrol 中查找控件

发布于 2024-07-24 15:02:48 字数 148 浏览 11 评论 0原文

您好,我在项目控制的数据模板中只有几个文本框。 当我将项目控件绑定到可观察集合时,我得到两个文本框。 但我需要根据每个文本框进行一些操作,我想使用某个 id 分别查找每个文本框。

任何人都可以帮助如何在 WPF 的 itemscontrol 中找到控件。

Hi i have few a single textbox within the the datatemplate for itemscontrol. When i bind the itemcontrols to a observable collection i get two text boxes. But i need to do some manipulations based on each of the text boxes for which i want to find each textbox seperatly using some id.

Can anybody help on how to find a control witin the itemscontrol in WPF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

燃情 2024-07-31 15:02:48

使用 ItemContainerGenerator,您可以获得为项目生成的容器,并向下遍历可视化树以找到您的 TextBox。 在 ItemsControl 的情况下,它将是 ContentPresenter,但 ListBox 将返回 ListBoxItem,ListView 将返回 ListViewItem 等。

ContentPresenter cp = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter;
TextBox tb = FindVisualChild<TextBox>(cp);
if (tb != null)
{
    // do something with tb
}

public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                return (T)child;
            }

            T childItem = FindVisualChild<T>(child);
            if (childItem != null) return childItem;
        }
    }
    return null;
}

如果需要,您还可以通过使用以下方式通过索引获取容器

itemsControl.ItemContainerGenerator.ContainerFromIndex(0);

Using the ItemContainerGenerator you can obtain the generated container for an item and traverse the visual tree downwards to find your TextBox. In the case of an ItemsControl it will be a ContentPresenter, but a ListBox will return a ListBoxItem, ListView a ListViewItem, etc.

ContentPresenter cp = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter;
TextBox tb = FindVisualChild<TextBox>(cp);
if (tb != null)
{
    // do something with tb
}

public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                return (T)child;
            }

            T childItem = FindVisualChild<T>(child);
            if (childItem != null) return childItem;
        }
    }
    return null;
}

You can also obtain the container by index if you want by using

itemsControl.ItemContainerGenerator.ContainerFromIndex(0);
浮生面具三千个 2024-07-31 15:02:48

谢谢布莱斯,我试图勾选向上箭头,但它说我的评分太低了! 对不起!

我修改了代码以返回给定类型的所有子项的所有列表,因为这是我所需要的,并且认为其他人可能会发现它有用。

再次感谢布莱斯,真的很有帮助 - 对于评级问题感到抱歉!

public static List<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        List<T> list = new List<T>();
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    list.Add((T)child);
                }

                List<T> childItems = FindVisualChildren<T>(child);
                if (childItems != null && childItems.Count() > 0)
                {
                    foreach (var item in childItems)
                    {
                        list.Add(item);
                    }
                }
            }
        }
        return list;
    }

Thanks Bryce, I tried to tick the up arrow but it says my rating is too low! Sorry!

I amended the code to return all a list of all the children of the given type as it was what I needed and thought someone else might find it useful.

Thanks again Bryce, really helpful - sorry about the rating thing!

public static List<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        List<T> list = new List<T>();
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    list.Add((T)child);
                }

                List<T> childItems = FindVisualChildren<T>(child);
                if (childItems != null && childItems.Count() > 0)
                {
                    foreach (var item in childItems)
                    {
                        list.Add(item);
                    }
                }
            }
        }
        return list;
    }
女中豪杰 2024-07-31 15:02:48

您可能想尝试使用 VisualTreeHelper。 ItemsControl 本身的属性仅允许您获取其绑定到的数据,而不是用于可视化数据的模板实例,而 VisualTreeHelper 允许您在 WPF 呈现可视化树时浏览它。

如果您(递归地)迭代父 ItemControl 的可视子项,那么您应该不会有任何困难找到您在屏幕上看到的文本框。

You may want to try using VisualTreeHelper. The properties on ItemsControl itself will only allow you to get the data its bound to, not the template instances used to visualize the data, while VisualTreeHelper allows you to browse around the visual tree as WPF has rendered it.

If you iterate through the parent ItemControl's visual children (recursively), you shouldn't have any difficulty locating the text boxes you are seeing on screen.

吲‖鸣 2024-07-31 15:02:48

另一个例子:

    private void DataGridBank_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        try
        {     
            switch (e.Key)
            {
                case Key.Down:

                    if ((DataGridBank.SelectedIndex + 1) <= DataGridBank.Items.Count)
                    {
                        DataGridBank.SelectedIndex = DataGridBank.SelectedIndex + 1;
                        FocusCell();
                    }
                    break;

                case Key.Up:

                    if ((DataGridBank.SelectedIndex - 1) >= 0)
                    {
                        DataGridBank.SelectedIndex = DataGridBank.SelectedIndex - 1;
                        FocusCell();
                    }
                    break;

                case Key.Enter:
                case Key.Tab:
                    FocusCell();                   

                    break;
            }
        }
        catch (Exception ex)
        {

        }
    }


    private void DataGridBank_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            if (DataGridBank.Items.Count > 0)
            {
                DataGridBank.SelectedIndex = 0;

                FocusCell();
            }

        }catch(Exception ex)
        {

        }
    }


    private void FocusCell()
    {
        var selectedRow = (DataGridRow)DataGridBank.ItemContainerGenerator.ContainerFromItem(DataGridBank.SelectedItem);

        var textImport = FindVisualChild<TextBox>(selectedRow);
        textImport.Focus();
        textImport.SelectAll();
    }


    public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    return (T)child;
                }

                T childItem = FindVisualChild<T>(child);
                if (childItem != null) return childItem;
            }
        }
        return null;
    }

Another example:

    private void DataGridBank_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        try
        {     
            switch (e.Key)
            {
                case Key.Down:

                    if ((DataGridBank.SelectedIndex + 1) <= DataGridBank.Items.Count)
                    {
                        DataGridBank.SelectedIndex = DataGridBank.SelectedIndex + 1;
                        FocusCell();
                    }
                    break;

                case Key.Up:

                    if ((DataGridBank.SelectedIndex - 1) >= 0)
                    {
                        DataGridBank.SelectedIndex = DataGridBank.SelectedIndex - 1;
                        FocusCell();
                    }
                    break;

                case Key.Enter:
                case Key.Tab:
                    FocusCell();                   

                    break;
            }
        }
        catch (Exception ex)
        {

        }
    }


    private void DataGridBank_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            if (DataGridBank.Items.Count > 0)
            {
                DataGridBank.SelectedIndex = 0;

                FocusCell();
            }

        }catch(Exception ex)
        {

        }
    }


    private void FocusCell()
    {
        var selectedRow = (DataGridRow)DataGridBank.ItemContainerGenerator.ContainerFromItem(DataGridBank.SelectedItem);

        var textImport = FindVisualChild<TextBox>(selectedRow);
        textImport.Focus();
        textImport.SelectAll();
    }


    public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    return (T)child;
                }

                T childItem = FindVisualChild<T>(child);
                if (childItem != null) return childItem;
            }
        }
        return null;
    }
花落人断肠 2024-07-31 15:02:48

如果您有数据网格和模板列,其中包含数据模板,
您可以使用以下代码示例

<DataGridTemplateColumn x:Name="photoPathColumn" Header="{x:Static resx:FrmResource.Photo}">
    <DataGridTemplateColumn.CellEditingTemplate x:Uid="keyelm">
        <DataTemplate x:Name="dodo">
            <StackPanel Orientation="Horizontal" Height="Auto">
                <TextBlock x:Name="photo" x:Uid="imageFile" Text="{Binding Path=PhotoPath}" />
                <Button x:Name="Browse" Content="..." Click="Browse_Click" />
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>

photoPathColumn.CellEditingTemplate.FindName("photo",photoPathColumn.GetCellContent(CustomersDataGrid.CurrentItem)) 

If you have data grid and template column, which contains data template,
you can use the following code sample

<DataGridTemplateColumn x:Name="photoPathColumn" Header="{x:Static resx:FrmResource.Photo}">
    <DataGridTemplateColumn.CellEditingTemplate x:Uid="keyelm">
        <DataTemplate x:Name="dodo">
            <StackPanel Orientation="Horizontal" Height="Auto">
                <TextBlock x:Name="photo" x:Uid="imageFile" Text="{Binding Path=PhotoPath}" />
                <Button x:Name="Browse" Content="..." Click="Browse_Click" />
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>

photoPathColumn.CellEditingTemplate.FindName("photo",photoPathColumn.GetCellContent(CustomersDataGrid.CurrentItem)) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文