WPF网格项和右对齐文本
我有一个WPF表单,我正在尝试制作一个简单的输入表单。两个标签、两个文本框和一个“提交”按钮。我有很好的布局,唯一的一件事,我不能得到的是我的“标签”是右对齐在他们的细胞。我尝试了TextAlign=“Right”和horizontalalign=“Right”,这两种方法都可以移动文本,覆盖我的文本框,而不仅仅是在单元格内移动。下面是窗口的XAMLWPF网格项和右对齐文本,wpf,user-interface,Wpf,User Interface,我有一个WPF表单,我正在尝试制作一个简单的输入表单。两个标签、两个文本框和一个“提交”按钮。我有很好的布局,唯一的一件事,我不能得到的是我的“标签”是右对齐在他们的细胞。我尝试了TextAlign=“Right”和horizontalalign=“Right”,这两种方法都可以移动文本,覆盖我的文本框,而不仅仅是在单元格内移动。下面是窗口的XAML <Window x:Class="MyWebKeepAliveDesktop.Login" xmlns="http://schema
<Window x:Class="MyWebKeepAliveDesktop.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyWebKeepAlive Desktop - Login" WindowStyle="None" AllowsTransparency="true" Height="200" Width="400" >
<Border Background="#50FFFFFF" CornerRadius="7" BorderBrush="{StaticResource WindowFrameBrush}" BorderThickness="2,0,2,2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition/>
</Grid.RowDefinitions>
<Border Background="{StaticResource WindowFrameBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
CornerRadius="5,5,0,0" Margin="-1,0,-1,0" MouseLeftButtonDown="DragWindow">
<Grid>
<TextBlock Foreground="White" FontWeight="Bold" VerticalAlignment="Center" Margin="10,2,10,2"
Text="MyWebKeepAlive Desktop Login"/>
<Button Content="X" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5" FontSize="7"
Width="15" Height="15" Padding="0" Command="ApplicationCommands.Close"/>
</Grid>
</Border>
<Grid Grid.Row="1" Width="350" Height="130" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="10" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<TextBlock TextAlignment="center" Text="Please provide your username/password that is used on the MyWebKeepAlive.com site to login." TextWrapping="Wrap" Grid.Row="0" Grid.ColumnSpan="2" />
<TextBlock Text="Username" FontWeight="Bold" Grid.Row="1" Grid.Column="0"/>
<TextBox Name="txtUsername" Width="150" Grid.Row="1" Grid.Column="1" />
<TextBlock Text="Password" FontWeight="Bold" Grid.Row="2" />
<TextBox Name="txtPassword" Width="150" Grid.Row="2" />
<Button Name="btnLogin" Grid.Row="4" Grid.ColumnSpan="2">
<TextBlock Text="Login" />
</Button>
</Grid>
</Grid>
</Border>
</Window>
您的网格只有一列。它需要两个来支持文本框的Grid.Column=1设置。因此,您需要添加一个
块。按照现在的XAML方式,WPF只是将两个控件都抛出到同一列中,因此您看到的行为就是这样的。我只是在学习WPF。如前所述,您的主要问题是缺少列定义
。我还将textalign=“Right”
属性添加到两个TextBlocks
<Grid Grid.Row="1" Width="350" Height="130" HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="25" />
<RowDefinition Height="25" />
<RowDefinition Height="10" />
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock TextAlignment="center" Text="Please provide your username/password that is used on the MyWebKeepAlive.com site to login." TextWrapping="Wrap" Grid.Row="0" Grid.ColumnSpan="2" />
<TextBlock Text="Username" TextAlignment="Right" FontWeight="Bold" Grid.Row="1" Grid.Column="0"/>
<TextBox Name="txtUsername" Width="150" Grid.Row="1" Grid.Column="1" />
<TextBlock Text="Password" TextAlignment="Right" FontWeight="Bold" Grid.Row="2" />
<TextBox Name="txtPassword" Width="150" Grid.Row="2" Grid.Column="1"/>
<Button Name="btnLogin" Grid.Row="4" Grid.ColumnSpan="2">
<TextBlock Text="Login" />
</Button>
</Grid>
在网格标记中添加:ShowGridLines=True这将帮助您查看行和列的限制Hanks Peter!Intellisense没有显示“ColumnDefinitions”,这让我很困惑。我以为这是我需要的,但我觉得我错过了什么!一切都好!