wpf scale to textBox, textBox can not display cursor when i click textBox

故事扮演 提交于 2019-11-29 16:20:41

A TextBox, especially a TextBox, is going to look bad and behave badly when scaled down. If you want your TextBox to look good and behave well, then use FontSize to reduce it and your font rendering and your cursor management will work better.

From an msdn answer I found:

The best workaround I work out is to apply an inverse transform on the TextBox and change the FontSize against the transform scale. You can wrap the TextBox with a Grid to maintain it's layout.

You can use the following code to see the effect. The FontSize in this sample is hardcoded to 10. You can use DataBinding to bind it to the scaletransform and use a converter to calculate a font size.

<Grid Background="AliceBlue">
<StackPanel>
<Border Height="100">
  <Canvas>
    <TextBox Canvas.Left="50" Canvas.Top="40" Width="500" Height="100" Background="Silver" Text="A Quick Red Fox Jumped Over A Lazy Brown Dog." FontSize="20"/>
    <Canvas.RenderTransform>
      <ScaleTransform ScaleX="0.5" ScaleY="0.5"/>
    </Canvas.RenderTransform>
  </Canvas>
</Border>
<Border Height="100">
  <Canvas>
    <Grid Canvas.Left="50" Canvas.Top="40" Width="500" Height="100">
      <TextBox Background="Silver" Text="A Quick Red Fox Jumped Over A Lazy Brown Dog." FontSize="10" LayoutTransform="{Binding ElementName=scale, Path=Inverse}"/>
    </Grid>
    <Canvas.RenderTransform>
      <ScaleTransform x:Name="scale" ScaleX="0.5" ScaleY="0.5"/>
    </Canvas.RenderTransform>
  </Canvas>
</Border>

You can see the full thread here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/aeaa3e28-a7da-4208-9676-771231c1a954?prof=required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!