WPF controls adaptive screen and wpf controls adaptive

Source: Internet
Author: User

WPF controls adaptive screen and wpf controls adaptive

If the interface is designed, the adaptive problem cannot be avoided. Let's talk about how to implement self-adaptation in wpf today. Let's first look at the effect: (add source code download at the end of this article)

 

The basic idea is to use the Grid to automatically fill and adapt the width. However, the layout of complex pages may be troublesome. We recommend that you use the DockPanel and Grid together, dockPanel can be used for some modules on the page that do not need to be adaptive. dock = "Top/Left/Right/Bottom". For the adaptive part (usually the main part in the middle of the Interface), use the DockPanel to automatically fill in and then apply the grid layout internally. In this way, a large area of interface can be split to reduce complexity.

For the style in the diagram at the beginning of the article, split the content into the upper and lower parts by using DockPanel. For TextBox and Button in the upper part, apply Grid and assign the column and column values to * to realize automatic width average distribution, if you want to achieve the effect of, you can set it to *, *, 2 *. For specific applications, refer to the following code.

<Grid> <Grid. rowDefinitions> <RowDefinition Height = "*"/> </Grid. rowDefinitions> <Grid. row = "0"> <Border BorderBrush = "CadetBlue" BorderThickness = "2"> <TextBlock TextAlignment = "Center" FontSize = "20" Text = ""/> </Border> </Grid> <Grid. row = "1"> <Grid. columnDefinitions> <ColumnDefinition Width = "*"/> <ColumnDefinition Width = "*"/> <ColumnDefinition Width = "3 *"/> </Grid. columnDefinitions> <Grid. column = "0"> <Border BorderBrush = "Red" BorderThickness = "1"> <TextBlock TextAlignment = "Center" FontSize = "20" Text = ""/> </Border> </Grid> <Grid. column = "1"> <Border BorderBrush = "Yellow" BorderThickness = "1"> <TextBlock TextAlignment = "Center" FontSize = "20" Text = ""/> </ border> </Grid> <Grid. column = "2"> <Border BorderBrush = "Blue" BorderThickness = "1"> <TextBlock TextAlignment = "Center" FontSize = "20" Text = ""/> </Border> </Grid> <Grid. row = "2"> <Grid. columnDefinitions> <ColumnDefinition Width = "*"/> <ColumnDefinition Width = "*"/> </Grid. columnDefinitions> <Grid. column = "0"> <Button Background = "MediumSpringGreen" FontSize = "20" Content = "match"/> </Grid> <Grid. column = "1"> <Button Background = "Coral" FontSize = "20" Content = "location"/> </Grid>

 

The DataGrid in the lower half may be relatively complex. You must not only set ColumnWidth = "Auto", but also add the following code on the backend to recalculate and update the column width:

            DataGridHeroList.Loaded += (s, e) =>            {                DataGridHeroList.Columns.AsParallel().ForEach(column =>                {                    column.MinWidth = column.ActualWidth;                    column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);                });            };

If you have any questions, please leave a message for further discussion ~~

Sample Code download link: https://pan.baidu.com/s/1boZCKLx

Password: fsq5

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.