简体   繁体   English

WPF文本在文本框中未对齐

[英]wpf text out of alignment in textbox

How to solve the text out of alignment in text box UI, but it was fine in copy to clipboard function to text file. 如何在文本框UI中解决文本不对齐的问题,但可以复制到剪贴板功能复制到文本文件中很好。

Thanks 谢谢

在此处输入图片说明

XAML: XAML:

     <Grid>
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Grid.Column="1" >
            <TextBox Name="txtServiceParameter" Width="550" Height="460" IsReadOnly="True" Margin="10, 10, 0, 0" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Text="{Binding DefaultText}" FontSize="12" FontFamily="Microsoft Sans Serif" HorizontalContentAlignment="Left"/>
            <Button Content="Copy to Clipboard" Width="150" Height="30" Margin="10, 10, 0, 0" Command="{Binding CopyToClipboardCommand}" />
        </StackPanel>
    </Grid>

C#: C#:

        defaultText += "Hello World - Class 1 Day 1 " + DateTime.Now.ToString("dd/MM/yyyy H:mm:ss");
        defaultText += Environment.NewLine;
        defaultText += Environment.NewLine;
        defaultText += Environment.NewLine;
        defaultText += "************************************************************************";
        defaultText += Environment.NewLine;
        defaultText += "Mathemathic Class";
        defaultText += Environment.NewLine;
        defaultText += "************************************************************************";
        defaultText += Environment.NewLine;
        defaultText += Environment.NewLine;
        defaultText += string.Format("{0, -3}, {1,-34}, {2}", "01", "Hello", "EEE910");
        defaultText += Environment.NewLine;
        defaultText += string.Format("{0, -3}, {1,-34}, {2}", "02", "Belo haha", "");          

        private void ExecuteCopyToClipboardCommand()
        {
          Clipboard.SetText(defaultText);
        }

Change the font family on the TextBox to one that is Monospaced . 将TextBox上的字体系列更改为Monospaced The problem is the font you are using. 问题是您使用的字体。 It does not have a fixed with for each character. 每个字符没有固定的。

通常,对于空格,我通常会使用某种空格字符的乘法,例如,如果需要10个空格,则将1-乘以2。这不是最好的答案,但它可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM