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

依然范特西╮ 提交于 2019-11-28 10:21:36

问题


First, I Zoom(ScaleTransform) the TextBox, then mouse to click on the TextBox. Sometimes can display the cursor, and sometimes can notdisplay the cursor. Looking for a solution to solution to the problem. I hope that I can show the cursor after I scale the TextBox.

 <Grid>
    <StackPanel>
        <TextBox Width="200"></TextBox>
        <TextBox Width="100"></TextBox>
        <TextBox Width="300"></TextBox>
        <TextBox Width="100"></TextBox>
        <TextBox Width="100"></TextBox>
        <TextBox Width="100"></TextBox>
    </StackPanel>
    <Grid.LayoutTransform>
        <ScaleTransform ScaleX="0.3" ScaleY="0.65"></ScaleTransform>
    </Grid.LayoutTransform>
</Grid>

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4551351/wpf-scale-to-textbox-textbox-can-not-display-cursor-when-i-click-textbox

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