Question about HorizontalAlignment.Right on window size changed

John Ostler 1 Reputation point
2021-02-12T13:48:42.513+00:00

Hello. I am making a wpf app. I have a button that is made out of a canvas. I added this button to the child of another canvas and i want it to stay on the right side of the parent and it does with the code I have written but when i change the size of the application the parent gets way bigger and the exit button doesn't stay on the right hand side which is a problem. How can i the child with horizonatal alignment. right on the right hand side always without adding code to a window size changed event? Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,768 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2021-02-15T03:14:23.13+00:00

    You can try to design your xaml like below:

     <Grid Width="{Binding RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor},Path=ActualWidth}"   
                  Height="{Binding RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor},Path=ActualHeight}"   
                  >  
                <Grid.RowDefinitions>  
                    <RowDefinition Height="4*"></RowDefinition>  
                    <RowDefinition Height="*"></RowDefinition>  
                </Grid.RowDefinitions>  
                <Canvas Background="Azure" Grid.Row="0">  
                </Canvas>  
                <Canvas Background="LightPink" Grid.Row="1">  
                    <Button Width="50" Content="Exit" Canvas.Right="25" Canvas.Bottom="40" />  
                </Canvas>  
            </Grid>  
    

    The result picture is:

    67983-2.gif

    Did I misunderstand, if I did, please post your xaml code for me to analyze.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.