2016-10-04 59 views
1

虽然在网站上有几个关于在WPF文本框中设置文本格式的问题,但是这个问题只发生在一小部分计算机上。如果还有类似的问题,请指点我!使用动态创建文本框的WPF文本格式问题

我有一个WPF应用程序,用于获取资源使用情况并在远程服务器上执行诊断/恢复任务。运行命令时,将在c#中创建一个文本框以显示结果输出。在大多数电脑上,文字打印出来很好。但是,在少数几台计算机和VDI上,我的团队使用输出似乎触及了边界并截断了输出的最后一列(请参阅屏幕截图)。

Normal output on success

Output seems to hit a boundary that doesn't occur on most computers

C#创建的TabItem和儿童,包括文本框:

 TabItem currentButtonTab = buttonTabControl.SelectedItem as TabItem; 
     TabItem resultsTab = new TabItem(); 
     TextBox resultsTabText = new TextBox(); 
     Grid resultsGrid = new Grid(); 
     Grid tabLabelGrid = new Grid(); 
     Button closeTabCmd = new Button(); 
     DockPanel tabPanel = new DockPanel(); 
     StackPanel tabLabelPanel = new StackPanel(); 
     Label tabLabel = new Label(); 

     resultsTabText.Style = (Style)Resources["txtStyle"]; 
     //resultsTabText.Margin = new Thickness(5); 
     //resultsTabText.TextAlignment = TextAlignment.Left; 
     resultsTabText.SetValue(Grid.ColumnProperty, 0); 
     resultsTabText.SetValue(Grid.ColumnSpanProperty, 2); 
     resultsTabText.HorizontalAlignment = HorizontalAlignment.Stretch; 
     resultsTabText.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden; 
     resultsTabText.VerticalScrollBarVisibility = ScrollBarVisibility.Auto; 
     //resultsTabText.FontFamily = new FontFamily("Consolas"); 
     resultsTabText.IsReadOnly = true; 
     resultsTabText.HorizontalContentAlignment = HorizontalAlignment.Stretch; 
     resultsTabText.MaxLines = 20; 
     resultsTabText.Tag = string.Format("resultsText"); 

     tabPanel.Margin = new Thickness(0); 
     tabPanel.SetValue(Grid.ColumnProperty, 0); 
     tabPanel.SetValue(Grid.ColumnSpanProperty, 2); 
     tabPanel.HorizontalAlignment = HorizontalAlignment.Stretch; 
     tabPanel.VerticalAlignment = VerticalAlignment.Stretch; 

     resultsTab.Padding = new Thickness(5, 0, 5, 0); 
     resultsTab.Content = resultsGrid; 
     resultsTab.Header = tabLabelGrid; 
     resultsTab.Name = string.Format("resultTab{0}", currentTabCount + 1); 
     resultsTab.Style = (Style)Resources["TabItemTemplate"]; 
     resultsTab.Focus(); 

     closeTabCmd.Click += clearButton_Click; 
     closeTabCmd.Tag = resultsTab.Name; 
     closeTabCmd.Margin = new Thickness(0); 
     closeTabCmd.Padding = new Thickness(1, -3, 1, -2); 
     closeTabCmd.VerticalAlignment = VerticalAlignment.Center; 
     closeTabCmd.HorizontalAlignment = HorizontalAlignment.Right; 
     closeTabCmd.Content = "X"; 
     closeTabCmd.Background = Brushes.WhiteSmoke; 
     closeTabCmd.Foreground = Brushes.Red; 

     tabLabelGrid.Margin = new Thickness(0,-5,0,-5); 
     tabLabelGrid.Children.Add(tabLabelPanel); 

     tabLabel.Content = computerName + "-" + buttonName; 
     tabLabel.Style = (Style)Resources["dynamicLabelStyle"]; 

     tabLabelPanel.Margin = new Thickness(0); 
     tabLabelPanel.Orientation = Orientation.Horizontal; 
     tabLabelPanel.Children.Add(tabLabel); 
     tabLabelPanel.Children.Add(closeTabCmd); 

     resultsTabControl.SelectionChanged += ResultsTabControl_SelectionChanged; 

     resultsTabControl.Items.Add(resultsTab); 
     resultsGrid.Children.Add(tabPanel); 
     tabPanel.Children.Add(resultsTabText); 

XAML表示应用在风格和家长的tabcontrol:

<Style x:Key="txtStyle" TargetType="{x:Type TextBox}"> 
     <Setter Property="Margin" Value="5"/> 
     <Setter Property="Padding" Value="10,5"/> 
     <Setter Property="FontFamily" Value="Consolas"/> 
     <Setter Property="Foreground" Value="Black"/> 
    </Style> 
    ... 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    ... 
    <StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"> 
     <StackPanel HorizontalAlignment="Right" Orientation="Horizontal"> 
      <Button x:Name="copyButton" Style="{StaticResource closeTabButton}" 
       Click="copyCmd_Click" Content="Copy Current Text"/> 
      <Button x:Name="closeTabsCmd" Style="{StaticResource closeTabButton}" 
       Click="closeTabsCmd_Click" Content="Close All"/> 
      <Button x:Name="copyCmd" Margin="5" Padding="2" Click="copyCmd_Click" Content="Copy" Visibility="Collapsed"/> 
     </StackPanel> 
     <TabControl x:Name="resultsTabControl" Visibility="Collapsed" Style="{StaticResource resultsControl}" 
      ButtonBase.Click="clearButton_Click" SelectionChanged="ResultsTabControl_SelectionChanged"> 
      <TabControl.Background> 
       <SolidColorBrush Color="#FFF9F9F9" Opacity="0.1"/> 
      </TabControl.Background> 
     </TabControl> 
    </StackPanel> 

我已经证实了这一点少于安装的.NET版本。这绝对让我疯狂。为了尽可能简短地保持这一点,我忽略了上面的代码,我认为这是无关紧要的,但是如果需要的话,会很乐意发布更多的代码。任何帮助或指导将非常感谢!

+0

您是否检查远程计算机的输出不在发生缠绕的位置?如果你使用powershell,我正在考虑这样的事情,只是为了排除可能性https://greiginsydney.com/viewing-truncated-powershell-output/ – rmc00

+0

也许这是一个视频卡驱动程序的问题? – auburg

+0

所以当你说“打印出来”,你的意思是“显示在屏幕上”? –

回答

0

有时PowerShell截断其输出非常类似于您所看到的。你能解决这个问题的方法之一是通过管道从字符串命令的输出设置宽度是这样的:

your-command | out-string -Width 160 

根据您的情况,还有其他的方法来控制的PowerShell的输出格式。请参阅https://greiginsydney.com/viewing-truncated-powershell-output/