简体   繁体   中英

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.

Thanks

在此处输入图片说明

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#:

        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 . The problem is the font you are using. It does not have a fixed with for each character.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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