You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF技术问询:如何遍历UniformGrid子项并获取UserControl的TextBox内容

解决UniformGrid中UserControl子项TextBox内容获取问题

方法一:给UserControl1添加公开属性暴露输入内容

这是最简洁且符合封装原则的方案,无需遍历控件树。

修改UserControl1的C#代码

添加依赖属性(支持WPF双向绑定,推荐用于需要绑定的场景):

public partial class UserControl1 : UserControl
{
    public static readonly DependencyProperty InputTextProperty =
        DependencyProperty.Register("InputText", typeof(string), typeof(UserControl1), new PropertyMetadata(string.Empty));

    public string InputText
    {
        get => (string)GetValue(InputTextProperty);
        set => SetValue(InputTextProperty, value);
    }

    public UserControl1()
    {
        InitializeComponent();
        // 将内部TextBox的Text与公开属性双向绑定
        txtInput.SetBinding(TextBox.TextProperty, new Binding("InputText") { Source = this, Mode = BindingMode.TwoWay });
    }
}

如果不需要绑定,也可以用普通只读属性:

public string InputText => txtInput.Text;

获取内容

直接遍历UniformGrid的Children集合,强转后访问属性:

foreach (var child in uniformGrid.Children)
{
    if (child is UserControl1 uc)
    {
        string userInput = uc.InputText;
        // 此处处理输入内容
    }
}

方法二:遍历控件树查找内部TextBox

如果无法修改UserControl1的代码,可以通过视觉树遍历找到目标TextBox。

编写控件树遍历辅助方法

private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);
        if (child is T targetControl)
            return targetControl;
        
        var nestedChild = FindVisualChild<T>(child);
        if (nestedChild != null)
            return nestedChild;
    }
    return null;
}

获取内容

遍历UniformGrid子项,调用辅助方法提取TextBox内容:

foreach (var child in uniformGrid.Children)
{
    if (child is UserControl1 uc)
    {
        TextBox inputBox = FindVisualChild<TextBox>(uc);
        if (inputBox != null)
        {
            string userInput = inputBox.Text;
            // 此处处理输入内容
        }
    }
}

方法三:MVVM绑定(适合长期项目)

如果采用MVVM架构,通过数据绑定直接从ViewModel获取内容,避免直接操作控件。

定义子项ViewModel

public class ItemViewModel : INotifyPropertyChanged
{
    private string _inputText;
    public string InputText
    {
        get => _inputText;
        set
        {
            _inputText = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

主ViewModel与XAML绑定

主ViewModel中定义集合:

public ObservableCollection<ItemViewModel> Items { get; set; } = new ObservableCollection<ItemViewModel>();

XAML使用ItemsControl包裹UniformGrid,绑定集合与子项属性:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:UserControl1 InputText="{Binding InputText, Mode=TwoWay}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

获取内容

直接遍历ViewModel集合:

foreach (var item in mainViewModel.Items)
{
    string userInput = item.InputText;
    // 此处处理输入内容
}

内容的提问来源于stack exchange,提问作者GinGo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 09:05:32