简体   繁体   中英

Controls (Buttons) resizing

Overflow Community, I have some problems with the sizing of my controls (more specific: buttons) Backstory: I'm using a program that requires that you connect to a specific website (which is hosted trough the program on my computer). Instead of opening my preferred web browser I want to use the WPF c# web browser tool in Microsoft Visual Studio. Now: I have already made it so the web browser tool can resize when the window is resized but resizing affects the buttons (2 in total). The buttons get smaller but they only grow to the set size (as made in the designer). XAML view:

<Window x:Name="PhantomBot_Browser" x:Class="PhantomBot_Browser_.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:PhantomBot_Browser_"
    mc:Ignorable="d"
    ResizeMode="CanResize"
    Title="PhantomBot Browser" Height="446.079" Width="731.863">
<Grid>
    <Grid x:Name="grid1" HorizontalAlignment="Left" VerticalAlignment="Top" Height="auto" Width="auto" >
        <Grid Margin="0,35,0,0">
            <Border HorizontalAlignment="Left" Width="auto" Height="auto">
                <WebBrowser x:Name="webbrowser1" ClipToBounds="True" Height="auto" Width="auto" />
            </Border>
        </Grid>
        <Grid Margin="10,10,663,385">
            <Button x:Name="refresh_button" Content="Refresh" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto" Height="auto" Click="refresh_button_Click"/>
        </Grid>
        <Grid Margin="66,10,624,385">
            <Button x:Name="gobutton" Content="GO" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto" Height="auto" Click="gobutton_Click"/>
        </Grid>
    </Grid>

</Grid>

MainWindow.xaml.cs view:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void gobutton_Click(object sender, RoutedEventArgs e)
    {
        webbrowser1.Navigate("http://localhost:25005/panel");
    }

    private void refresh_button_Click(object sender, RoutedEventArgs e)
    {
        webbrowser1.Refresh();
    }

    private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        //Grid.Width = this.ActualWidth; Remove the two slashes at the front and minus/add values ONLY if you need it.
        grid1.Height = e.NewSize.Height - 100;
        grid1.Width = e.NewSize.Width - 300;
        webbrowser1.Height = grid1.Height - 300;
        //refresh_button.Width = 51;
        //gobutton.Width = 34;
    }

}

I want that the buttons stay at their position and their size.

UPDATE : (some pictures) Picture when the program is at a small size (look at the buttons at the top left corner!) https://i.stack.imgur.com/OZa0r.png

picture when the program is at a bigger size (buttons are okay) https://i.stack.imgur.com/LT3yd.png

dont mind the connection error, its on purpose

Grid 101: Supports adding rows and columns and setting height/width for each, which can be relative to containing element, or fixed. For example:

<Grid> 
<Grid.RowDefintions> 
 <RowDefintion Height="Auto"/> Will receive height per child in that row.
 <RowDefintion Height="5"/> Height is 5 pixels 
 <RowDefinition Height=""/> Height is remaining space (Height in this case) 
 <RowDefintion Height="2"/> Height is twice the height the preceeding row 
</Grid.RowDefintions> 
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM