【问题标题】:How to get access to text from all the textboxes( where textboxes are created dynamically) in windows phone runtime如何在 Windows Phone 运行时中从所有文本框(动态创建文本框)访问文本
【发布时间】:2015-04-14 12:17:21
【问题描述】:

我一直在尝试从文本框集合中获取文本,这些文本框是通过使用项目控件将集合绑定到堆栈面板来动态创建的,该控件位于单独的用户控件中,我正在 Windows Phone 运行时的页面上加载该控件。

下面是我的 UserControl 的代码:

<UserControl
    x:Class="CfMobility.UserControls.CredentialsUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CfMobility.UserControls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <ScrollViewer>
    <ItemsControl ItemsSource="{Binding SelectedCategorySettings}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel IsTapEnabled="False">
                    <TextBlock Text="{Binding SettingKey}" Style="{ThemeResource BaseTextBlockStyle}"></TextBlock>
                    <TextBox  Text="{Binding SettingValue}" Width="300px"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    </ScrollViewer>
</UserControl>

我在另一个页面中有一个内容控件,如下所示:

<ContentControl x:Name="Container" Grid.Row="1" Margin="19,10,19,0">
        </ContentControl>

当我导航到页面时,我将此内容控件绑定到上面的堆栈面板。

如您所见,我已使用 ItemsScroll 将“SelectedCategorySettings”集合绑定到 StackPanel,它根据集合显示文本框的数量。在这里我无法弄清楚是否要将页面上显示的所有文本框中的文本保存到 json 文件中,如何访问在上述场景中动态显示的所有文本框的文本?

PS:注意items是控件在单独的用户控件中。

提前致谢

【问题讨论】:

    标签: windows-phone windows-phone-8.1 winrt-xaml


    【解决方案1】:

    您应该为 SelectedCategorySettings 使用 ObservableCollection,并且包含 SettingKey 和 SettingValue 的模型类应该实现 INotifyPropertyChanged 接口,如以下示例代码所示。如果你做得正确,那么无论 UI 中发生什么变化(在你的情况下,文本框的文本变化)都会自动反映在 ObservableCollection 中的模型对象中。如果你有兴趣了解更多关于它是如何工作的,我建议你搜索 Windows Phone 开发中的 mvvm 设计模式。

    public class Setting : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private string _settingKey;
    
        public string SettingKey
        {
            get { return _settingKey; }
            set { 
                _settingKey = value;
                OnPropertyChanged("SettingKey");
            }
        }
    
        private string _settingValue;
    
        public string SettingValue
        {
            get { return _settingValue; }
            set {
                _settingValue = value;
                OnPropertyChanged("SettingValue");
            }
        }
    
    
        public virtual void OnPropertyChanged(string propertyName)
        {
            var propertyChanged = PropertyChanged;
            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    【讨论】:

    • 谢谢!但我的问题是一次访问所有文本框内容以保存在本地存储中的某个文件中。正如您在上面看到的“SettingValue”文本框。假设我已经在“SelectedCategorySettings”中有一些值,这意味着页面上会有很多文本框。现在,假设我已经编辑了其中一些文本框并希望将它们保存回某个文件,例如本地存储中的 JSON。由于这些文本框是动态创建的,我想访问要保存到文件中的内容。我该怎么做?
    • 是的,我了解您的问题。但问题是您正在尝试访问由 ItemsControl 创建的文本框,就像使用 xaml 按名称或类似名称创建的文本框一样。但这不是您在 Windows Phone 中执行此操作的方式。您需要使用 mvvm 模式,就像我在上面的答案中解释的那样。如果您正确执行,“SelectedCategorySettings”中的值将在用户键入和更改文本框中的值时由 ui 自动更新。然后您可以从“SelectedCategorySettings”本身获取新值。因此,您无需显式访问文本框即可获取新值。
    【解决方案2】:

    你需要的是这样的方法:

    var textBoxes = AllTextBoxes(this);
    
    public List<TextBox> AllTextBoxes(DependencyObject parent)
    {
        var list = new List<TextBox>();
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            if (child is TextBox)
            {
                list.Add(child as Control);
                continue;
            }
            list.AddRange(AllChildren(child));
        }
        return list;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-08
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 2014-06-19
      相关资源
      最近更新 更多