2016-10-04 37 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/