B
B
bagos2018-03-29 21:24:30
WPF
bagos, 2018-03-29 21:24:30

How to access dynamically created objects using datatemplate in xaml?

ReportParameter class

public class ReportParameter
    {
        public string ColumnName { get; set; }
        public string ParameterType { get; set; }
    }

In ParameterList in a loop is added
ParameterList.Add(new ReportParameter() { ColumnName = param.ColumnName,  ParameterValue = param.ParamValue });

in xaml listview with datatemplate
<ListView ItemsSource="{Binding ParameterList}">
            <ListView.Resources>
                <DataTemplate DataType="{x:Type generic:ReportParameter}">
                    <StackPanel>
                        <Label Content="{Binding Path=ColumnName}" />
                        <TextBox Text="{Binding Path=ParameterValue}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.Resources>
        </ListView>

When the window loads, textboxes are drawn.
It is assumed that the data in these textboxes will change and be passed on. How can I access each of the textboxes if they are created dynamically?
In the case of static properties, I did this
private string _propety;
        public string Property
        {
            get { return _propety; }
            set
            {
                if (value == _propety) return;
                _propety = value;
                OnPropertyChanged("Property");
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lam0x86, 2018-03-29
@bagos

No need to access TextBoxes, this violates the MVVM principle (although it is possible through hacks like visual tree traversal). Moreover, in the mode

VirtualizingStackPanel.VirtualizationMode="Recycling"
(which is used by default) not all elements of the ParameterList array will correspond to TextBoxes.
Here I do not understand. What does it mean to pass on?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question