在网格中的按钮上调整TextBlock

时间:2015-06-03 09:23:07

标签: wpf xaml button alignment textblock

网格上有两个按钮。在右侧按钮上应该显示左对齐的一些文本和右对齐的另一个文本。到目前为止,在我所有的尝试中,两个字符串都位于按钮的中心,而不是左右。

一旦解决了这个问题,我需要在文本和按钮左/右边框之间留出一些空间。 谁可以帮忙?

我的XAML代码到目前为止开始:

2015-06-03 17:16:15.705 PSM-InPatient[10744:799454] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<PSM_InPatient.DocLoginViewController 0x7fbf3bc75990> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key docPassword.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000105b37f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010767bbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000105b37b79 -[NSException raise] + 9
    3   Foundation                          0x0000000105f4f7b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
    4   CoreFoundation                      0x0000000105a81e80 -[NSArray makeObjectsPerformSelector:] + 224
    5   UIKit                               0x0000000106688c7d -[UINib instantiateWithOwner:options:] + 1506
    6   UIKit                               0x00000001064e7f98 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
    7   UIKit                               0x00000001064e8588 -[UIViewController loadView] + 109
    8   UIKit                               0x00000001064e87f9 -[UIViewController loadViewIfRequired] + 75
    9   UIKit                               0x00000001064e8c8e -[UIViewController view] + 27
    10  UIKit                               0x0000000106a8a41e -[_UIFullscreenPresentationController _setPresentedViewController:] + 65
    11  UIKit                               0x00000001064c4429 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 105
    12  UIKit                               0x00000001064f4a41 -[UIViewController _presentViewController:withAnimationController:completion:] + 1746
    13  UIKit                               0x00000001064f6d81 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132
    14  UIKit                               0x00000001064f6ca5 -[UIViewController presentViewController:animated:completion:] + 229
    15  UIKit                               0x00000001063c48be -[UIApplication sendAction:to:from:forEvent:] + 75
    16  UIKit                               0x00000001064cb410 -[UIControl _sendActionsForEvents:withEvent:] + 467
    17  UIKit                               0x00000001064ca7df -[UIControl touchesEnded:withEvent:] + 522
    18  UIKit                               0x000000010640a308 -[UIWindow _sendTouchesForEvent:] + 735
    19  UIKit                               0x000000010640ac33 -[UIWindow sendEvent:] + 683
    20  UIKit                               0x00000001063d79b1 -[UIApplication sendEvent:] + 246
    21  UIKit                               0x00000001063e4a7d _UIApplicationHandleEventFromQueueEvent + 17370
    22  UIKit                               0x00000001063c0103 _UIApplicationHandleEventQueue + 1961
    23  CoreFoundation                      0x0000000105a6d551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    24  CoreFoundation                      0x0000000105a6341d __CFRunLoopDoSources0 + 269
    25  CoreFoundation                      0x0000000105a62a54 __CFRunLoopRun + 868
    26  CoreFoundation                      0x0000000105a62486 CFRunLoopRunSpecific + 470
    27  GraphicsServices                    0x0000000109c2e9f0 GSEventRunModal + 161
    28  UIKit                               0x00000001063c3420 UIApplicationMain + 1282
    29  PSM-InPatient                       0x00000001055a33de top_level_code + 78
    30  PSM-InPatient                       0x00000001055a341a main + 42
    31  libdyld.dylib                       0x0000000107e55145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

不要责怪我不把这些东西放在ResourceDictionary中。只是想让这个例子变得简单。

1 个答案:

答案 0 :(得分:1)

您需要将HorizontalContentAlignment="Stretch"添加到Button,以便内容占用全部空间。之后,要在文本和按钮边框之间留出空间,只需在第一个和最后一个位置再添加两个网格列。

XAML:

<UserControl x:Class="MyNamespace.MyClass"
     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" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="900">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <Button>
        <TextBlock Text="left button"></TextBlock>
    </Button>
    <Button Grid.Column="1" HorizontalContentAlignment="Stretch">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="10"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="left" Grid.Column="1"></TextBlock>
            <TextBlock Text="right" Grid.Column="3"></TextBlock>
        </Grid>
    </Button>
</Grid>