动态创建的TextBox

时间:2016-10-04 13:52:16

标签: c# wpf xaml

虽然网站上有一些关于格式化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版本,都会发生这种情况。这绝对让我疯狂。为了保持尽可能简短,我省略了上面的代码,我认为这是无关紧要的,但如果需要,很乐意发布更多代码。任何帮助或指导将不胜感激!

1 个答案:

答案 0 :(得分:0)

有时,PowerShell会截断其输出,与您所看到的非常相似。解决这个问题的一种方法是通过输出字符串输出命令的输出来设置宽度,如下所示:

your-command | out-string -Width 160

根据您的方案,还有其他方法可以控制PowerShell输出的格式。见https://greiginsydney.com/viewing-truncated-powershell-output/