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