There is some set of Facts (Title, Text). The user can complement it. I need to place all these facts on the grid. The problem is that it is impossible to calculate the size of the block with the text for its positioning. In the end, everything moves.

void LoadFacts(int FactId) { grid.Children.Clear(); double indent = 0; foreach (Fact fact in facts) { Label title = new Label() { Content = fact.Title, Margin = new Thickness(30, indent, 0, 0), VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Left, FontSize = 20, }; TextBlock text = new TextBlock() { Text = fact.Text, Margin = new Thickness(30, 50 + indent, 0, 0), VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Left, FontSize = 16, TextWrapping = TextWrapping.Wrap, Background = Brushes.AliceBlue, }; text.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); indent += text.DesiredSize.Height; grid.Children.Add(title); grid.Children.Add(text); } grid.Height = indent; } 

Result

  • 2
    Don't you think that in the great WPF, which has a cool markup language, XAML - placing objects through code is blasphemy and perversion? - EvgeniyZ
  • It is impossible to predict in advance the number and content of facts - Vlad Ogogo
  • 2
    For this there are such things as Binding and ListBox type elements with their own style - EvgeniyZ
  • And why all the same DesiredSize is not working as needed? - Vlad Ogogo
  • There are special tools for reports and documents, but as far as I know, they only work for viewing. Specify what you mean by "User can add it." - user227049

1 answer 1

What you write - I think a perversion! Firstly, WPF without MVVM , bindings and their other elements is like a refrigerator without a motor in the cold, like freezing, but not like it can ... Secondly, according to the MVVM rules, your code should not know that it has a window open , in the window there is a Lable or some other things, this is not correct.

How to do it right:

  • Implement competent Binding , how to do it - there are many examples, you can see my recent answer .
  • Next, you need to decide what you want to see on the page. This can be implemented for example using a ListBox , and you can using an ItemsControl . Take, for example, ItemsControl , the simplest one:

     <ItemsControl ItemsSource="{Binding MyCollection}"> <ItemsControl.ItemTemplate> <DataTemplate> <Label Content="{Binding}" /> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 

What do we see here? We have the ItemsControl itself, which with the help of the ItemsSource tied to a certain MyCollection collection (about it later), inside we redefine the template and set the usual Lable for the test, which is tied to the value in the MyCollection collection (here, in fact, which you wish, be it a cap, a text, an author, or other parameters).

All that remains is for us to add a collection and work with it:

 public ObservableCollection<string> MyCollection { get; set; } = new ObservableCollection<string>(); 

Well, add to it:

 MyCollection.Add("hello"); MyCollection.Add("word!"); 

At the exit, if everything is done correctly, you will get something like this:

Result

That's all. If you are trying to write something, write it right from the very beginning, otherwise there will be sooo many problems. Good luck in mastering!

  • Thank you very much. And if there should be a page change? How to implement it correctly? - Vlad Ogogo
  • @VladOgogo here it is better to ask a separate question on this topic, I can give you only the logic of how it should be about, but not the code or something else. Logically, you should have a common base of all "posts" and one tied collection (as in the answer), you should also have a team / transition team. Well, by clicking on them you add to the linked collection, say 10 elements, skipping 10 previous ones (for example, this will be the 2nd page). Skipping and taking the required number is Skip(10).Take(10) from the collection. But again, maybe there are better solutions, I have not come across this. He said how I would have done - EvgeniyZ
  • 2
    @VladOgogo But in no case do not do what you did in the question. Do not mock the poor WPF ) - EvgeniyZ