Элементы сетки WPF и выравниваемый справа текст - PullRequest
4 голосов
/ 28 мая 2009

У меня есть форма WPF, где я пытаюсь сделать простую форму ввода. Две метки, два текстовых поля и кнопка «Отправить». У меня довольно хороший макет, единственное, что я не могу получить, это чтобы мои «Ярлыки» были правильно выровнены внутри их ячеек. Я пробовал и TextAlign = "Right", и HorizontialAlign = "Right", который перемещает текст ВСЕ до конца, перекрывая мое текстовое поле, а не просто перемещаясь внутри ячейки. Ниже XAML для окна.

<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>

Ответы [ 2 ]

11 голосов
/ 29 мая 2009

Ваша сетка имеет только один столбец, как написано. Для поддержки установки Grid.Column = 1 для текстовых полей потребуется два. Таким образом, вам нужно добавить блок <ColumnDefinitions>. С XAML, как сейчас, WPF просто выбрасывает оба элемента управления в один и тот же столбец, и, следовательно, поведение, которое вы видите.

2 голосов
/ 29 мая 2009

Вот что я придумал. Просто изучаю WPF сам. Как упоминалось PeterAllenWebb , ваша главная проблема в том, что вам не хватает ColumnDefinitions. Я также добавил атрибуты TextAlignment="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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...