Good evening. How can I dynamically add controls ( TextBox'ы ) to TextBox'ы ? For example, there are two objects Line Circle

 public class Line:IObjects { private string _nameObject; public Point StartPoint(){get;set;} public Point EndPoint(){get;set;} public string NameObject{{return "Line";}{_nameObject = value;}} } public class Circle:IObjects { private string _nameObject; public Point CenterPoint(){get;set;} public string NameObject{{return "Line";}{_nameObject = value;}} } interface IObject { string NameObject{get;set;} } 

When I receive interface objects in View, I need to display the coordinate parameters, since in Line , two parameters are used, and in Circle one determines how many TextBox'ов need to be displayed to change the parameters of each object. Creating pre- TextBox'ы in UC not logical, because if necessary we should be able to create the required number of elements depending on the edited object.

  • And, where is MVVM? - Streletz
  • @Streletz plans to use dynamically added elements with this pattern, i.e. from the ViewModel transfer the data to the View , but they need to be correctly reflected, that is, there are several options (coordinates come, for example, the coordinates of the line (coordinates of the beginning and end of the line) and the circle (coordinates of the center)), but it’s not nice to do two TextBox for the circle, option to change the visibility the same is not suitable not productively, suddenly it will be necessary to add the polyhedron n-face. - KJfe
  • According to your description it is absolutely not clear what you want. reformulate the question - tym32167
  • one
    You would first learn the following concepts and what they are used for: ObservableCollection , INotifyPropertyChanged , ItemsControl with ItemsControl.ItemTemplate . The first pair of links in Google is enough. Essentially: 1) to link both of your classes in the new LineAndCircle class. 2) Create an ObservableCollection<LineAndCircle> . 3) In the interface, create an ItemControl with the ItemTemplate you ItemTemplate . 4) Bind the ItemSource your ItemControl to an ObservableCollection<LineAndCircle> - John
  • 2
    @John why should I connect these two classes if I add a thousand elements of shapes there (a polygon with 6 sides, triangles, and any crap, and suddenly three-dimensional shapes are added, broken), that I will link them all, nonsense, for this I have an interface that I add to the collection, then I get these parameters from it. - KJfe

1 answer 1

See if you want to display a list of objects, for this you need to really use the ItemsControl and bind the list to an ItemsSource .

Usually all objects are the same, and then an ItemTemplate is appropriate for displaying an object. ( Here is an example .) But if your list is heterogeneous, this approach will not work. There are several different solutions. You can use ItemTemplateSelector . But if objects have different types, then such a simple solution will do:

 <ItemsControl ItemsSource="{Binding тут-имя-вашей-коллекции}"> <ItemsControl.Resources> <DataTemplate DataType="{x:Type vm:Line}"> <!-- тут шаблон для отображения линии --> </DataTemplate> <DataTemplate DataType="{x:Type vm:Circle}"> <!-- тут шаблон для отображения окружности --> </DataTemplate> </ItemsControl.Resources> </ItemsControl> 

Here is an example for DataContext = new object[] { 1, 2, "три", 4, "пять" } :

 <ItemsControl ItemsSource="{Binding}"> <ItemsControl.Resources> <DataTemplate DataType="{x:Type sys:String}"> <TextBlock Text="{Binding StringFormat='String {0}'}"/> </DataTemplate> <DataTemplate DataType="{x:Type sys:Int32}"> <TextBlock Text="{Binding StringFormat='Int {0}'}"/> </DataTemplate> </ItemsControl.Resources> </ItemsControl> 

Result:

a rabbit went out for a walk


If you need to process objects of different types in the "editor", you can use the same technique, but with ContentPresenter 'om:

 <ContentPresenter Content="{Binding CurrentObject}"> <ContentPresenter.Resources> <DataTemplate DataType="{x:Type vm:Line}"> <!-- тут шаблон для редактирования линии --> </DataTemplate> <DataTemplate DataType="{x:Type vm:Circle}"> <!-- тут шаблон для редактирования окружности --> </DataTemplate> </ItemsControl.Resources> </ItemsControl> 
  • @VledD This is of course good, but I was interested in the question of how to organize data change for different objects, that is, when choosing an object, a field would open where it was dynamically filled in depending on the type of object and it would be possible to change the data. - KJfe
  • @KJfe: Added. - VladD
  • The question is, how can I add a variable (vm) for my objects, or how should I be crammed into resources? - KJfe
  • @KJfe: Uh, in any such resources? View has no right to set itself VM. ru.stackoverflow.com/a/562586/10105 - VladD
  • On account of this, I know, I just can not understand vm is a link to the ViewModel? - KJfe