I know we can stretch item to the right or down using ColumnSpan and RowSpan
However I got an Item I want to display in grid cell and have it centered according to this cell center, so it will span both right and left in case of text, here I provide an image of how it currently looks, “12345” text can’t fit the column and is displayed as “234”, is there a way to make 1 & 5 visible leaving all columns untouched?
Text can be changed to anything and has to be centered correctly, I can hack a way to display 12345 this way , but need a solution that will work with any text, just in case,and textBlock grid.Column has to be left untouched 1 (or 2 to be precise for my case) , not 0 for this picture case.
Solution 4
So what you actually want is a way to center the text dynamically if I now understand correctly. You don’t want to have to work out fiddling with the padding yourself, you want a number that works for everything? I sure hope you appreciate this! 🙂
A couple of notes. It uses the
.ActualWidthof the columns in the Grid. You can change this of course to.Width, however you may run into problems when you’re usingColumnDefinition Width="30*"because I’m quite sure you won’t get a double as the result then. Secondly, this function doesn’t set the padding on the object you pass for two reasons:– You can take the returned
Thicknessand add it to the.Paddingalready applied to aControlso that you don’t destroy it’s formatting.– You can pass any
FrameworkElement, whereas onlyControlshave padding. Perhaps you could add the returned thickness to the.Marginproperty instead, or even create a container and apply padding within that?Finally, you could easily combine the two
for loopswithin the function, but I left them seperate from clarity.Cheers.