0

我是 WPF 的新手。我在设置窗口屏幕时遇到问题。我有两台分辨率不同的电脑。

我正在一台 PC 上开发,但由于屏幕分辨率的原因,我无法在另一台 PC 上运行它。

我在 XAML 中尝试了以下代码:

SizeToContent="WidthAndHeight".

我也试过下面的代码:

MainWindow a = new MainWindow();
            a.WindowState = WindowState.Maximized;

            a.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            a.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
4

3 回答 3

1

尝试删除

SizeToContent="WidthAndHeight"

并放

WindowState="Maximized"

在您的 XAML 中

于 2018-05-04T10:36:46.420 回答
0

在主窗口中设置 -HorizontalAlignment="Stretch"和同样的事情VerticalAlignment="Stretch"

于 2018-05-04T06:41:00.750 回答
0

要遵循的步骤

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
            }
于 2019-12-24T09:53:03.097 回答