XAML text block cannot be centered

I have a PopUp element in my XAML, inside which I have a Grid with a TextBlock inside.

The problem is that the text and the grid cannot be centered with horizontal and vertical alignment ...

XAML :

<Popup x:Name="Popup" Height="30px" VerticalAlignment="Top">
      <Grid Height="30px" 
            Background="Green" 
            Width="{Binding ActualWidth, ElementName=Popup}">
            <TextBlock VerticalAlignment="Center" 
                       HorizontalAlignment="Center" 
                       Foreground="White" 
                       Text="Änderungen wurden übernommen">
            </TextBlock>
      </Grid>
</Popup>

      

Visual Studio preview (which looks exactly the same as I want): enter image description here Actual result: enter image description here TextBox inside the green PopUp is not centered as you can see. And I don't know why, maybe I missed something, but everything looks good to me.

+3


source to share


1 answer


You can manually subscribe to SizeChanged

yours Popup

as follows:

Popup.SizeChanged += (s,e) => PopupGrid.Width = Popup.ActualWidth;

      


Update

I used to create a popup UserControl

that exposes a property MessageText

, so I can pass dynamic text and another flag IsOpen

to toggle its visibility. I also disabled him from hitting testing and let him fire me after a few seconds.



Hiding and showing must be done with the help of Storyboard

which is encapsulated inside the control.

In the end, this one MessageControl

should look like this:

<local:MessageControl IsOpened="{x:Bind MyToggle.IsOn, Mode=OneWay}" Message="Die Änderung wurde übernommen!" />

      

I have included a sample project here and this is how it looks like -

enter image description here

+2


source







All Articles