【问题标题】:Orientation of WPF status bar and label are in wrong place when the window is maximised窗口最大化时WPF状态栏和标签的方向错误
【发布时间】:2019-07-24 13:33:03
【问题描述】:

在我的串口 WPF 应用程序中,当我最大化窗口时,标签(BaudRate、Parity、DataBits 和 stopBit)和 StatusBar(用于显示 PortStatus)位于错误的位置。

我希望获得专家的建议来解决我的问题。

所以请告诉我是否可以对我的 xaml 文件进行任何更改/编辑,以使这些方向与正常窗口模式下的位置相同。

<Window
        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:SerialReadAndWrite"
        xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="ATR220ReadAndWrite.MainWindow"
        mc:Ignorable="d"
        Title="ATR220ReadAndWrite" Height="450" Width="800">
    <Grid Margin="0,0,-71,-107">
        <ComboBox x:Name="ComPortComboBox"  HorizontalAlignment="Left" Height="14" Margin="115,20,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="ComPort_SelectionChanged" DropDownOpened="ComPort_DropDownOpened" VerticalContentAlignment="Stretch" IsSynchronizedWithCurrentItem="False" FontSize="9" FontFamily="Arial"/>
        <Label Content="PortNumber :" HorizontalAlignment="Left" Margin="21,14,0,0" VerticalAlignment="Top"/>
        <Label Content="PortSettings :" HorizontalAlignment="Left" Margin="21,58,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.546,3.143" FontSize="10" FontFamily="Arial"/>
        <Image HorizontalAlignment="Left" Height="26" Margin="359,14,0,0" VerticalAlignment="Top" Width="29" RenderTransformOrigin="-0.23,0.36"/>
        <ComboBox x:Name="BaudRateComboBox" HorizontalAlignment="Left" Margin="115,84,0,0" VerticalAlignment="Top" Width="98" Height="17" SelectionChanged="BaudRate_SelectionChanged"  IsEnabled="False" IsReadOnly="True" FontSize="9" FontFamily="Arial">
            <System:String>115200</System:String>
            <System:String>57600</System:String>
            <System:String>38400</System:String>
            <System:String>19200</System:String>
            <System:String>14400</System:String>
            <System:String>9600</System:String>
            <System:String>4800</System:String>
        </ComboBox>
        <ComboBox x:Name="ParityComboBox" HorizontalAlignment="Left" Height="17" Margin="308,86,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="Parity_SelectionChanged"  IsEnabled="False" FontSize="9" FontFamily="Arial">
            <System:String>Even</System:String>
            <System:String>Mark</System:String>
            <System:String>None</System:String>
            <System:String>Odd</System:String>
            <System:String>Space</System:String>
        </ComboBox>
        <ComboBox x:Name="DataBitsComboBox" HorizontalAlignment="Left" Height="17" Margin="115,125,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="DataBits_SelectionChanged"  IsEnabled="False" FontSize="8" FontFamily="Arial">
            <System:String>5</System:String>
            <System:String>6</System:String>
            <System:String>7</System:String>
            <System:String>8</System:String>
        </ComboBox>
        <ComboBox x:Name="StopBitsComboBox" HorizontalAlignment="Left" Height="17" Margin="308,123,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="StopBits_SelectionChanged" IsEnabled="False" FontSize="10" FontFamily="Arial">
            <System:String>One</System:String>
            <System:String>Two</System:String>
            <System:String>OnePointFive</System:String>
        </ComboBox>
        <Label Content="Baudrate :" HorizontalAlignment="Center" Margin="47,81,753,423" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="63"/>
        <Label Content="Parity :" HorizontalAlignment="Center" Margin="234,81,573,423" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="56"/>
        <Label Content="DataBits :" HorizontalAlignment="Center" Margin="48,123,753,382" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="62"/>
        <Label Content="StopBit :" HorizontalAlignment="Center" Margin="238,123,567,382" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Height="22" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="10" FontStyle="Italic" Width="58"/>
        <Button x:Name="ConnectButton" Content="Connect" HorizontalAlignment="Left" Margin="308,20,0,0" VerticalAlignment="Top" Width="75" Click="ConnectButton_Click" IsEnabled="False" Height="14" FontSize="10" FontFamily="Arial"/>
        <Label Content="ProtocolCustomWndow :" HorizontalAlignment="Left" Margin="21,172,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.546,3.143" FontSize="10" FontFamily="Arial"/>
        <ComboBox x:Name="ProtocolTypeComboBox"  HorizontalAlignment="Left" Height="14" Margin="141,177,0,0" VerticalAlignment="Top" Width="98" SelectionChanged="ProtocolTypeComboBox_SelectionChanged" VerticalContentAlignment="Stretch" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" FontSize="9" FontFamily="Arial" IsEnabled="False">
            <System:String>WLink</System:String>
        </ComboBox>
        ***<StatusBar HorizontalAlignment="Left" Height="28" Margin="0,396,0,0" VerticalAlignment="Top" Width="794">
            <StatusBarItem FontSize="9" FontFamily="Arial" TextOptions.TextHintingMode="Fixed">
                <TextBlock Name= "PortStatus"/>
            </StatusBarItem>
        </StatusBar>***
    </Grid>
</Window>

附上正确和错误的图片。

错误的位置 Wrong Position

正确的位置 Correct position

【问题讨论】:

  • 停止使用边距!!!,为您的 Grid 使用列定义和行定义。
  • 我是 WPF 新手,请告诉我如何在我的 xaml 文件中使用它们?
  • 我已经更新了我的答案,您也应该更新您的帖子,而不是发布您尝试作为答案的代码。
  • 1.删除您的答案,因为您应该更新您的问题。 2.我需要更多关于你第二个问题的信息,你最好再问一个问题,我很乐意回答,但记得把更多信息放在那里。
  • 我根据您的要求添加了另一个问题,但我不知道如何与您分享

标签: wpf orientation statusbar


【解决方案1】:

试试这样的:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <TextBlock Text="PortNumber :" HorizontalAlignment="Center"/>
    <ComboBox FontSize="9" FontFamily="Arial" Grid.Column="1">
        <ComboBoxItem IsSelected="True">COM 3</ComboBoxItem>
    </ComboBox>
    <Button Content="Connect" Grid.Row="0" Grid.Column="3"></Button>
    <TextBlock Text="PortSettings :" FontSize="10" FontFamily="Arial" Grid.Row="1" HorizontalAlignment="Center"/>
    <TextBlock Text="Baudrate :" FontStyle="Italic" Grid.Row="2" HorizontalAlignment="Center"/>
    <ComboBox IsEnabled="False" IsReadOnly="True" FontSize="9" FontFamily="Arial" Grid.Row="2" Grid.Column="1">
        <System:String>115200</System:String>
        <System:String>57600</System:String>
        <System:String>38400</System:String>
        <System:String>19200</System:String>
        <System:String>14400</System:String>
        <System:String>9600</System:String>
        <System:String>4800</System:String>
    </ComboBox>
    <TextBlock Text="Parity :" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Center"/>
    <ComboBox Grid.Row="2" Grid.Column="3"/>
</Grid>  

特别注意Grid.RowGrid.Column的使用,你需要在这些元素上设置水平对齐,但这会告诉你如何在WPF中定位东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多