locked
How do you programmatically change the height of a button giving its width? RRS feed

  • Question

  • This works for making a square button:

    Height = "{Binding Path=ActualWidth, RelativeSource={RelativeSource Self}}"

    I would like a aspect ratio of 1 to 5 though. Something like this:

    Height = "{Binding Path=ActualWidth/5.0, RelativeSource={RelativeSource Self}}"
    

    That does not compile though.

    I have attempted to use c# to make a square button:

    Button button = (Button)FindName("ButtonStart");
                var myBinding = new Binding("Height")
                {
                    Source = button.ActualWidth
                };
                button.SetBinding(Button.HeightProperty, myBinding);

    This doesn't make the button square. 

    Is it possible to set the aspect ratio of a button using data binding?

               
    • Moved by CoolDadTx Thursday, August 6, 2020 2:16 PM WPF related
    Thursday, August 6, 2020 1:21 AM

All replies

  • Hello,

    Please use the following forum for WPF

    https://docs.microsoft.com/en-us/answers/topics/windows-wpf.html


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, August 6, 2020 1:30 AM
  • Hi JohnGlen244,

    Thank you for posting here.

    This simple code should work.

    ButtonStart.Height = ButtonStart.Width / 5;

    You can also refer to the suggestions in this link to implement the aspect ratio setting in xaml.

    How perform calculation on a XAML Binding value: reverse it, multiply it, subtract from it or add to it?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 6, 2020 6:25 AM
  • Where do I put that code? The button has a height of 0 in the Window constructor.
    Thursday, August 6, 2020 1:59 PM
  • You don't have permission to access "http://docs.microsoft.com/answers/questions/ask.html" on this server.

    Reference #18.445f1ab8.1596724359.83070f9c

    Thursday, August 6, 2020 2:33 PM