none
metro textbox GetRectFromCharacterIndex 无法取到值

    问题

  • 文本框里有字, 返回Rect了, 但是里面怎么是异常喔

    需求: 实现TextBox里文字的垂直居中

    2012年5月12日 4:01

答案

全部回复

  • 通过 Debug.WriteLine(a.Bottom); 还是可以输出正确内容的。

    我去咨询一下,因为这个问题几个月前我在英文论坛那边也遇到了,只是没有重现出来。现在有了,所以我会继续去跟进看看是不是是一个产品Bug. http://social.msdn.microsoft.com/Forums/en-us/winappswithcsharp/thread/09396c28-1974-4469-bec8-b48e647b7d64


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年5月14日 10:11
    版主
  • 如果实现垂直居中可以直接该template就可以做到

    直接饮用这个样式 或者在blend 选中TextBox  右键 Edit Template->Edit a copy  选中 ContentElement 修改Layout 属性即可

    <Style x:Key="TextBoxStyle1" TargetType="TextBox">
    			<Setter Property="MinWidth" Value="{StaticResource TextControlThemeMinWidth}"/>
    			<Setter Property="MinHeight" Value="{StaticResource TextControlThemeMinHeight}"/>
    			<Setter Property="Foreground" Value="{StaticResource TextBoxForegroundThemeBrush}"/>
    			<Setter Property="Background" Value="{StaticResource TextBoxBackgroundThemeBrush}"/>
    			<Setter Property="BorderBrush" Value="{StaticResource TextBoxBorderThemeBrush}"/>
    			<Setter Property="BorderThickness" Value="{StaticResource TextControlBorderThemeThickness}"/>
    			<Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/>
    			<Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/>
    			<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>
    			<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
    			<Setter Property="Padding" Value="{StaticResource TextControlThemePadding}"/>
    			<Setter Property="Template">
    				<Setter.Value>
    					<ControlTemplate TargetType="TextBox">
    						<Grid>
    							<Grid.Resources>
    								<Style x:Name="DeleteButtonStyle" TargetType="Button">
    									<Setter Property="Template">
    										<Setter.Value>
    											<ControlTemplate TargetType="Button">
    												<Grid>
    													<VisualStateManager.VisualStateGroups>
    														<VisualStateGroup x:Name="CommonStates">
    															<VisualState x:Name="Normal"/>
    															<VisualState x:Name="PointerOver">
    																<Storyboard>
    																	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundElement">
    																		<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxButtonPointerOverBackgroundThemeBrush}"/>
    																	</ObjectAnimationUsingKeyFrames>
    																	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
    																		<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxButtonPointerOverBorderThemeBrush}"/>
    																	</ObjectAnimationUsingKeyFrames>
    																	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="GlyphElement">
    																		<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxButtonPointerOverForegroundThemeBrush}"/>
    																	</ObjectAnimationUsingKeyFrames>
    																</Storyboard>
    															</VisualState>
    															<VisualState x:Name="Pressed">
    																<Storyboard>
    																	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundElement">
    																		<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxButtonPressedBackgroundThemeBrush}"/>
    																	</ObjectAnimationUsingKeyFrames>
    																	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
    																		<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxButtonPressedBorderThemeBrush}"/>
    																	</ObjectAnimationUsingKeyFrames>
    																	<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="GlyphElement">
    																		<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxButtonPressedForegroundThemeBrush}"/>
    																	</ObjectAnimationUsingKeyFrames>
    																</Storyboard>
    															</VisualState>
    															<VisualState x:Name="Disabled">
    																<Storyboard>
    																	<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundElement"/>
    																	<DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BorderElement"/>
    																</Storyboard>
    															</VisualState>
    														</VisualStateGroup>
    													</VisualStateManager.VisualStateGroups>
    													<Border x:Name="BorderElement" BorderBrush="{StaticResource TextBoxButtonBorderThemeBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
    													<Border x:Name="BackgroundElement" Background="{StaticResource TextBoxButtonBackgroundThemeBrush}" Margin="{TemplateBinding BorderThickness}">
    														<TextBlock x:Name="GlyphElement" Foreground="{StaticResource TextBoxButtonForegroundThemeBrush}" FontFamily="{StaticResource SymbolThemeFontFamily}" HorizontalAlignment="Center" Text="&#xE0A4;" VerticalAlignment="Center"/>
    													</Border>
    												</Grid>
    											</ControlTemplate>
    										</Setter.Value>
    									</Setter>
    								</Style>
    							</Grid.Resources>
    							<Grid.ColumnDefinitions>
    								<ColumnDefinition Width="*"/>
    								<ColumnDefinition Width="Auto"/>
    							</Grid.ColumnDefinitions>
    							<VisualStateManager.VisualStateGroups>
    								<VisualStateGroup x:Name="CommonStates">
    									<VisualState x:Name="Disabled">
    										<Storyboard>
    											<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundElement">
    												<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxDisabledBackgroundThemeBrush}"/>
    											</ObjectAnimationUsingKeyFrames>
    											<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
    												<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxDisabledBorderThemeBrush}"/>
    											</ObjectAnimationUsingKeyFrames>
    											<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
    												<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TextBoxDisabledForegroundThemeBrush}"/>
    											</ObjectAnimationUsingKeyFrames>
    										</Storyboard>
    									</VisualState>
    									<VisualState x:Name="Normal">
    										<Storyboard>
    											<DoubleAnimation Duration="0" To="{StaticResource TextControlBackgroundThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundElement"/>
    											<DoubleAnimation Duration="0" To="{StaticResource TextControlBorderThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BorderElement"/>
    										</Storyboard>
    									</VisualState>
    									<VisualState x:Name="PointerOver">
    										<Storyboard>
    											<DoubleAnimation Duration="0" To="{StaticResource TextControlPointerOverBackgroundThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundElement"/>
    											<DoubleAnimation Duration="0" To="{StaticResource TextControlPointerOverBorderThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BorderElement"/>
    										</Storyboard>
    									</VisualState>
    									<VisualState x:Name="Focused"/>
    								</VisualStateGroup>
    								<VisualStateGroup x:Name="ButtonStates">
    									<VisualState x:Name="ButtonVisible">
    										<Storyboard>
    											<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DeleteButton">
    												<DiscreteObjectKeyFrame KeyTime="0">
    													<DiscreteObjectKeyFrame.Value>
    														<Visibility>Visible</Visibility>
    													</DiscreteObjectKeyFrame.Value>
    												</DiscreteObjectKeyFrame>
    											</ObjectAnimationUsingKeyFrames>
    										</Storyboard>
    									</VisualState>
    									<VisualState x:Name="ButtonCollapsed"/>
    								</VisualStateGroup>
    							</VisualStateManager.VisualStateGroups>
    							<Border x:Name="BackgroundElement" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" Margin="{TemplateBinding BorderThickness}"/>
    							<Border x:Name="BorderElement" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="2"/>
    							<ScrollViewer x:Name="ContentElement" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsTabStop="False" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" Padding="{TemplateBinding Padding}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="Disabled" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    							<Button x:Name="DeleteButton" BorderThickness="{TemplateBinding BorderThickness}" Grid.Column="1" FontSize="{TemplateBinding FontSize}" IsTabStop="False" Style="{StaticResource DeleteButtonStyle}" Visibility="Collapsed" VerticalAlignment="Stretch"/>
    						</Grid>
    					</ControlTemplate>
    				</Setter.Value>
    			</Setter>
    		</Style>

    2012年5月23日 10:14