Looping over XAML defined labels

后端 未结 6 1295
傲寒
傲寒 2021-01-18 08:58

I have a WPF application with many labels.

         


        
相关标签:
6条回答
  • 2021-01-18 09:13

    You could add all those Label to a List<Label> within the form's constructor.

    It is a tedious work, but you'll only have to do it once.

    List<Label> labels = new List<Label>();
    labels.Add(label1);
    // etc.
    
    // loop
    for (int i = 0; i < labels.Count; i++)
        labels[i].Text = i.ToString();
    
    // alternative loop
    foreach (Label label in labels)
        label.Text = "test";
    
    0 讨论(0)
  • 2021-01-18 09:21

    You can make it with binding !

    xaml view:

    <ItemsControl ItemsSource="{Binding List}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding Value, Mode=TwoWay}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    in code behind in the constructor:

    DataContext = new ViewModelClass();
    

    in ViewModelClass:

    class ViewModelClass : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private void NotifyPropertyChanged(string name)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
            }
    
            private ObservableCollection<StringObject> _List = new ObservableCollection<StringObject>();
            public ObservableCollection<StringObject> List
            {
                get { return _List; }
                set
                {
                    _List = value;
                    NotifyPropertyChanged("List");
                }
            }
    
            public ViewModelClass()
            {
                List = new ObservableCollection<StringObject>
                    {
                        new StringObject {Value = "your"},
                        new StringObject {Value = "list"},
                        new StringObject {Value = "of"},
                        new StringObject {Value = "string"}
                    };
            }
        }
    
        public class StringObject
        {
            public string Value { get; set; }
        }
    

    Be careful with a collection with type string it doesn't work, you have to use an object => StringObject

    0 讨论(0)
  • 2021-01-18 09:24

    How about this?

    List<Label> labelL = new List<Label>();
    
    for(int i = 1; i<40 ;i++)
        labelL.Add(new Label{ Name = "label"+i, Content = i*10 });
    

    The end result will be this.

    label1.Text = 10;
    label2.Text = 20;
    label3.Text = 20;
    ...
    ...
    
    0 讨论(0)
  • 2021-01-18 09:26

    If your labels are all named consistently, you can do it like this:

    var numberOfLabels = 40;
    
    for(int i = 1; i <= numberOfLabels; i++)
    {
        var labelName = string.Format("label{0}", i);
        var label = (Label) this.FindName(labelName);
        label.Content = i * 10;
    }
    
    0 讨论(0)
  • 2021-01-18 09:28

    Using this code

    public static IEnumerable<T> FindVisualChildren<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)
                {
                    yield return (T)child;
                }
    
                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }
    

    You can enumerate all controls by type.

    foreach (Label lbl in FindVisualChildren<Label>(window))
    {
        // do something with lbl here
    }
    
    0 讨论(0)
  • 2021-01-18 09:31

    If you work with binding it is easy. You just have to keep your label content in an ObservableCollection<string> on your ViewModel. And then, you can do whatever you want with them, in your case iteration.

    Edit 1:

    Also your xaml should be something like:

            <ItemsControl ItemsSource="{Binding MyLabelValues}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <sdk:Label Content="{Binding Mode=TwoWay}"></sdk:Label>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
    
    0 讨论(0)
提交回复
热议问题