Visibility Developed, leaving a small space in the user interface

Given the following xaml structure

<Grid> <StackPanel> <TextBlock> <TextBlock>Block 1</TextBlock> <TextBlock>Block 2</TextBlock> </TextBlock> <TextBlock> <TextBlock Visibility="Collapsed">Block 3</TextBlock> <TextBlock>Block 4</TextBlock> </TextBlock> <TextBlock> <TextBlock>Block 5</TextBlock> <TextBlock>Block 6</TextBlock> </TextBlock> </StackPanel> </Grid> 

I expect this conclusion:

 Block 1 Block 2 Block 4 Block 5 Block 6 

But I get it

 Block 1 Block 2 Block 4 Block 5 Block 6 

Where is the space in front of Block 4 , and how do I leave it?

+6
source share
1 answer

Using Snoop , you can see that it is from ContainerVisual , which wraps your child TextBlock s. Because TextBlock trying to display a collection of derived Inline objects (this is abstracted out altogether), it should wrap your TextBlock in ContainerVisual .

Since these are actual objects ordered by the parent TextBlock , and they don’t even have the Visibility property, you have to reorganize your code a bit. I assume that you are trying to bind to several separate properties and want to display them in one TextBlock. You can fix this in several ways:

  • Make an IMultiValueConverter and pass a bunch of properties that control what to show
  • Build complex string in ViewModel
  • Replace the external TextBlock with the StackPanel with Orientation="Horizontal"

I would recommend the latter, as this is the easiest.

 <StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock>Block 1</TextBlock> <TextBlock>Block 2</TextBlock> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Visibility="Collapsed">Block 3</TextBlock> <TextBlock>Block 4</TextBlock> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock>Block 5</TextBlock> <TextBlock>Block 6</TextBlock> </StackPanel> </StackPanel> 
+5
source

Source: https://habr.com/ru/post/952594/


All Articles