要遵循的步骤
1.创建Xaml页面
<Page x:Class="Mainwin.TrkView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Mainwin"
mc:Ignorable="d"
Title="TrkView" Loaded="Page_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto">
</RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto">
</ColumnDefinition>
<ColumnDefinition Width="auto">
</ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel x:Name="trkSetWidthAdjust" Grid.Row="0" Grid.Column="0" Margin="20 20 20 20" Background="White" Height="auto" Width="auto">
<DockPanel>
<TextBlock x:Name="TrkHeader" Margin="25,10,5,0" FontSize="16" FontWeight="Bold" TextAlignment="Left" HorizontalAlignment="Left" Foreground="#2a3457">TRKS</TextBlock>
<TextBlock HorizontalAlignment="Right" Margin="0 0 35 0">
<Button x:Name="Trks" Style="{StaticResource PinkButtonStyle}" Margin="0 8 5 0" Click="btnTrksExport_Click" ><TextBlock FontSize="12" FontWeight="Bold" Margin="10,2,10,0" Text="Export" TextAlignment="Center" Height="20" /></Button>
</TextBlock>
</DockPanel>
<StackPanel Margin="20 10 20 20">
<DataGrid AutoGenerateColumns="False" CanUserResizeRows="False" BorderBrush="Gray" ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto" Height="auto" RowHeight="25" HorizontalAlignment="left" Name="TrkAnalysisDataGrid" VerticalAlignment="Top" Width="auto">
<DataGrid.Resources>
<Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#2a3457" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Height" Value="25" />
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderThickness" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="100" Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTextColumn Header="Department" Width="100" Binding="{Binding Department}"></DataGridTextColumn>
<DataGridTextColumn Header="Place" Width="150" Binding="{Binding Place}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</StackPanel>
</Grid>
</Page>
2.在代码后面,在InitializeComponent之后,绑定下面的代码
//By following the below code, it help to accommodate the UI to fit into the All screen Resolution
public TrkView()
{
InitializeComponent();
Window MainWindow = System.Windows.Application.Current.MainWindow;
PresentationSource MainWindowPresentationSource = PresentationSource.FromVisual(MainWindow);
Matrix m = MainWindowPresentationSource.CompositionTarget.TransformToDevice;
var DpiWidthFactor = m.M11;
var DpiHeightFactor = m.M22;
double ScreenHeight = SystemParameters.PrimaryScreenHeight * DpiHeightFactor;
double ScreenWidth = SystemParameters.PrimaryScreenWidth * DpiWidthFactor;
this.trkSetWidthAdjust.Width = ScreenWidth - 240;//assigning the width for the panel
this.TrkAnalysisDataGrid.Height = ScreenHeight - 160;//assigning the Height for the DataGrid
}