0

I'm basically having the same problem as this guy. The thing is, I want there to be a certain number of rectangles on the screen, depending on the screen size/resolution. Now the first comment on that question says "creating UI elements in procedural code in XAML-based technologies is discouraged, cumbersome, probably a bad idea, and just plain wrong".

So is there another way in which I can achieve my goal?

One way I can think of would be to have a minimum of 3x3 rectangles, but add UI code for 6x3 and make the last 3 rows visibility collapsed. In app load, I could figure out if I could fit more rows, and selectively make them visible. I'd still like to know if there is a cleaner approach.

I won't paste my code here since it's very similar to the linked question, but I will if asked, and it helps to clarify things a bit.

Community
  • 1
  • 1
Freakishly
  • 1,533
  • 5
  • 32
  • 61

1 Answers1

1

I understand the general sentiment that a XAML app should declare as much of the UI as possible in the XAML. But I also believe it's possible to be too dogmatic about things. If one genuinely has a scenario where the layout of UI elements one would otherwise select during design-time is unknown until run-time, then why not populate the UI at run-time based on that information?

Barring some clearly-written and compelling argument detailing why run-time creation of UI elements is actually bad, I'm going to keep on doing it when appropriate. I will happily design as much of the UI in advance, but if there are parts that depend on information known only at run-time, I will write code to do that.

A person can wind up with a lot of really awful code and maintenance hassles trying to shoe-horn a specific design goal into some constrictive, prescriptive dogma.

I went ahead and posted an answer to the question you've linked to. I don't know if that's actually something you needed, but if so you can look there. It seems to me that the main question here is "is it really so wrong to configure my UI at run-time?" and my answer to that is, as you can see, "no, not necessarily".

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Thank you for sharing your thoughts. Your solution worked perfectly. For posterity, if you want to adapt to rectangles, you can set the HorizontalAlignment and VerticalAlignment to Stretch if you want to take up all the available space like me, or set the Rectangle Width and Height manually. – Freakishly Nov 11 '14 at 07:05
  • -1. As stated by the OP in the comment above, all he needed was to set a couple of properties in XAML. You're talking about a solution which does not conform to WPF's accepted, well known best practices. Also, since you've not shown any code, your post can be considered a useless rant, rather than a proper answer to the OP's problem. – Federico Berasategui Nov 11 '14 at 22:27
  • @HighCore: you are welcome to your opinion. But there's no code here because there's no point in it (just as there was no point in the OP including code in his question). And you've completely misunderstood the OP's comment...setting those properties doesn't address his _question_, it's just a way to format the individual grid cells in the context of the _actual answer_. You should read more carefully before you wave that down-vote stick around, especially when the OP himself has indicated the answer is useful. – Peter Duniho Nov 11 '14 at 22:39
  • @PeterDuniho you should learn what an `ItemsControl` is before telling people they need a bunch of horrible procedural code in WPF to create dynamic items-based UIs. – Federico Berasategui Nov 11 '14 at 22:40
  • @HighCore: I believe the expression is "put up or shut up". – Peter Duniho Nov 11 '14 at 22:44
  • @HighCore: this question doesn't appear to be a duplicate of any of the ones you've linked to, so it could probably do with an XAML-based answer, even if it's just a copy-and-paste of an existing answer. – Harry Johnston Nov 11 '14 at 23:25