简体   繁体   English

是否有可能获得所有具有相同名称的标签,但名称末尾的数字不同?

[英]Is there a possibility to get all Labels with the same name except different number at the end of the name?

I designed a window with WPF, and so I used a lot of Labels.我设计了一个 window 和 WPF,所以我使用了很多标签。 My question now is, if it is possible to get all Labels that are named like this: lblinch, lblinch1, lblinch2... .我现在的问题是,是否可以获得所有这样命名的标签:lblinch, lblinch1, lblinch2...。 Personally, I would like to store them in an array or in an arraylist.就个人而言,我想将它们存储在数组或 arraylist 中。 The Labels are not in the same parent-element except for "Window" and the first "Grid".除了“Window”和第一个“Grid”之外,标签不在同一个父元素中。

<Label Content="inch" Name="lblinch2" Grid.Row="0" Grid.Column="2" />
                            <Label Content="Distance between Beams Manual Input" Grid.Row="1" Grid.Column="0" />
                            <TextBox Name="tbDistanceBeamsManualInput" Width="{x:Static local:Constants.TOOL_WIDTH}" Grid.Row="1" Grid.Column="1" Margin="0,5,0,5" Background="Yellow" />
                            <Label Content="inch" Name="lblinch3" Grid.Row="1" Grid.Column="2" />
                            <Label Content="Bolt Hole Center to Column Wall * =" Grid.Row="2" Grid.Column="0" />
                            <TextBox Name="tbBHoleCenterToColumnWall" Width="{x:Static local:Constants.TOOL_WIDTH}" Grid.Row="2" Grid.Column="1" Margin="0,5,0,5" Background="Yellow" />
                            <Label Content="inch" Grid.Row="2" Name="lblinch4" Grid.Column="2" />

First, name your parent Grid .首先,命名您的父Grid ("RootGrid" in this case.) (在这种情况下为“RootGrid”。)

<Grid x:Name="RootGrid">
    <Label x:Name="lblinch1" />
    <TextBlock />
    <Label x:Name="lblinch2" />
    <TextBox />
    <Label x:Name="lblinch3" />
    <Button />
    <Label x:Name="lblinch4" />
    <Label x:Name="lblinch5" />
</Grid>

Then use the VisualTreeHelper to get the children and filter them by their names.然后使用VisualTreeHelper获取孩子并按他们的名字过滤他们。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }

    private List<Label> LabelControls { get; } = new();

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        int count = VisualTreeHelper.GetChildrenCount(this.RootGrid);

        for (int i = 0; i < count; i++)
        {
            if (VisualTreeHelper.GetChild(this.RootGrid, i) is Label label &&
                label.Name.StartsWith("lblinch") is true)
            {
                LabelControls.Add(label);
            }
        }
    }
}

Here's what I'm doing to find controls of a specific type with a part of their x:Name :这是我正在做的事情,以查找具有x:Name一部分的特定类型的控件:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfApp4
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //find labels that have "lbl" in their name
            var labels = GetControlsByName<Label>(LabelsContainer, "lbl", new List<Label>());

            //do something with them
            foreach (var label in labels)
            {
                Console.WriteLine(label.Name);
            }
        }

        private List<T> GetControlsByName<T>(Visual root, string nameSearchTerm, List<T> result) where T : FrameworkElement
        {
            var childrenCount = VisualTreeHelper.GetChildrenCount(root);

            for (var i = 0; i < childrenCount; i++)
            {
                var childVisual = (Visual)VisualTreeHelper.GetChild(root, i);

                if (childVisual is T control && control.Name.Contains(nameSearchTerm))
                {
                    result.Add(control);
                }

                GetControlsByName(childVisual, nameSearchTerm, result);
            }

            return result;
        }
    }
}

XAML XAML

<Window
    x:Class="WpfApp4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WpfApp4"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid x:Name="LabelsContainer">
        <Label x:Name="lbl1" />
        <TextBlock x:Name="tbl1" />
        <Label x:Name="lbl2" />
        <Label x:Name="lbl3" />
    </Grid>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取具有相同名称的所有属性 - Get all attributes with the same name 获取所有richTextBoxes并加载具有相同名称的文本文件 - Get all the richTextBoxes and load textfiles with the same name 如何获得具有相同名称的所有属性? - How to get all attributes with the same name? Tridion 2009 - 使用Interops - 是否有可能为同一个名称添加多个setConditions - Tridion 2009 - Using Interops - Is there a possibility to add multiple setConditions for the same Name 更新查询将更新除名称之外的所有记录 - Update query updates all records except the name 除了ID和Name以外,所有文件属性均为null。 - All file properties are null except Id and Name? 是否有可能使用C#从Facebook获得一个人的名字 - is there any possibility to get the name of a person from facebook with C# C#。 如何为表单中的所有标签赋予相同的名称,因此,如果对象与标签之一绑定,它将显示一些内容 - C#. How can I give all labels in my form the same name, so if an object bounds with one of the labels that it will show something 如何从以相同名称开头的不同行获取数据? - how to get data from different line starting with same name? 如何使用linq从xml获取具有相同名称的不同值 - How to get different values with same name from xml with linq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM