【问题标题】:in C# wpf how to loop through a grid and get all the labels inside of the grid在 C# wpf 中,如何循环遍历网格并获取网格内的所有标签
【发布时间】:2014-04-01 18:53:23
【问题描述】:

所以你知道如何在 c# 中使用普通形式循环遍历面板并获取其中的所有标签吗? 所以你可以这样做:

foreach(Label label in Panel.Controls)

有没有办法为网格做到这一点?像

foreach(Lable lable in Grid)

所以这个 foreach 可以在一个像这样传递网格对象的函数中

private void getLabels(Grid myGrid)
{
  foreach(Label label in myGrid)
}

如果我这样做,它会告诉我“错误 CS1579:foreach 语句无法对 'System.Windows.Controls.Grid' 类型的变量进行操作,因为 'System.Windows.Controls.Grid' 不包含 ' 的公共定义GetEnumerator'"

我现在知道还有另一种方法吗?

任何帮助将不胜感激。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    normal forms - WPF 我们在 2014 年以正常的方式处理 .Net Windows UI。

    如果您正在使用 WPF,则需要摒弃从古老技术中获得的所有概念,理解并接受 The WPF Mentality

    基本上,您不会“迭代” WPF 中的任何内容,因为绝对没有必要这样做。

    UI 的职责是显示数据,而不是存储数据或操作数据。因此,您需要显示的任何数据都必须存储在适当的数据模型或 ViewModel 中,并且 UI 必须使用适当的 DataBinding 来访问它,而不是程序代码。

    例如,假设您有一个Person 类:

    public class Person
    {
        public string LastName {get;set;}
    
        public string FirstName {get;set;}
    }
    

    您需要将 UI 的 DataContext 设置为以下列表:

    //Window constructor:
    public MainWindow()
    {
        //This is required.
        InitializeComponent();
    
        //Create a list of person
        var list = new List<Person>();
    
        //... Populate the list with data.
    
        //Here you set the DataContext.
        this.DataContext = list;
    }
    

    然后您将希望在 ListBox 或另一个基于 ItemsControl 的 UI 中显示:

    <Window ...>
        <ListBox ItemsSource="{Binding}">
    
        </ListBox>
    </Window>
    

    然后您会想要使用 WPF 的 Data Templating 功能来定义如何在 UI 中显示 Person 类的每个实例:

    <Window ...>
       <Window.Resources>
          <DataTemplate x:Key="PersonTemplate">
              <StackPanel>
                  <TextBlock Text="{Binding FirstName}"/>
                  <TextBlock Text="{Binding LastName"/>
              </StackPanel>
          </DataTemplate>
       </Window.Resources>
    
       <ListBox ItemsSource="{Binding}"
                ItemTemplate="{StaticResource PersonTemplate}"/>
    </Window>
    

    最后,如果您需要在运行时更改数据,并将这些更改反映(显示)在 UI 中,您的 DataContext 类必须Implement INotifyPropertyChanged

    public class Person: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }
    
        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                OnPropertyChanged("LastName");
            }
        }
    
        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName");
            }
        }
    }
    

    最后,您遍历 List&lt;Person&gt; 并更改数据项的属性,而不是操纵 UI:

    foreach (var person in list)
        person.LastName = "Something";
    

    同时保留 UI。

    【讨论】:

      【解决方案2】:

      遍历 Grid.Children 并将所有内容转换为 Label。如果它不为空,则您找到了一个标签。

      【讨论】:

        猜你喜欢
        • 2018-08-08
        • 2023-03-20
        • 1970-01-01
        • 2012-03-25
        • 2013-03-19
        • 1970-01-01
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多