【发布时间】:2019-04-24 15:02:34
【问题描述】:
不错且简单的问题,但对于像我这样的 PowerShell 人来说,谷歌上的 c# 响应是一个雷区(我需要学习!)
我有一个使用 XAML 创建的富文本框,我希望能够添加不同颜色字体的行。
大部分 Saipen 结果表明 $formLogReport.SelectionColor 但不存在此类属性。
我实际上发现了一些可行的方法,但它有点矫枉过正,超出了我对 PowerShell 的了解——我不愿意使用我不理解的代码。
http://vcloud-lab.com/entries/powercli/powershell-gui-format-text-on-textbox-and-richtextbox
作为参考,下面的代码使用了提供的链接中的函数。
[void][System.Reflection.Assembly]::LoadWithPartialName( 'presentationframework' )
[void][System.Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )
[xml]$xaml = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="RichTextBox Example" Height="530" Width="740" >
<Grid Name="GridName">
<Label Name="SetupLabel" Content="Setup type" FontSize="11" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="30,25,30,30" Height="25" Width="320" />
<ComboBox Name="SetupList" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="30,55,30,30" Height="25" Width="320" />
<Label Name="SubsiteLabel" Content="Text in here will be a different size" FontSize="11"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,265,30,30" Height="25" Width="320" />
<TextBox Name="SubsiteBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,295,0,0"
Height="25" Width="320" TextWrapping="Wrap" TextAlignment="Left" VerticalContentAlignment="Center" />
<Label Name="StuffLabelLabel" Content="Enter Stuff to show up" FontSize="11"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,345,30,30" Height="25" Width="320" />
<TextBox Name="StuffBox" FontSize="11" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="30,375,0,0" Height="25" Width="320" TextWrapping="Wrap" TextAlignment="Left"
VerticalContentAlignment="Center" />
<Label Name="LogLabel" Content="Log..." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="11"
Margin="390,25,0,0" Height="25" Width="320" />
<RichTextBox Name="LogReport" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="390,55,0,0"
Height="345" Width="300" >
<FlowDocument>
<Paragraph>"HI THERE"
<Run Text=""/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button Name="GoButton" Content="Go!" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="625,435,0,0"
Height="25" Width="65" IsEnabled="False" />
</Grid>
</Window>
'@
$reader = ( New-Object System.Xml.XmlNodeReader $xaml )
try {
$Form = [Windows.Markup.XamlReader]::Load( $reader )
}
catch {
Write-Warning "Unable to parse XML, with error: $( $Error[0] )`n "
}
#===========================================================================
# Load XAML Objects / Form Changes & Conditions
#===========================================================================
$xaml.SelectNodes( "//*[@Name]") | ForEach-Object { Set-Variable -Name "form$( $_.Name )" -Value $Form.FindName( $_.Name ) }
function Format-RichTextBox {
#https://msdn.microsoft.com/en-us/library/system.windows.documents.textelement(v=vs.110).aspx#Propertiesshut
param (
[parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[System.Windows.Controls.RichTextBox]$RichTextBoxControl,
[String]$Text,
[String]$ForeGroundColor = 'Black',
[String]$BackGroundColor = 'White',
[String]$FontSize = '12',
[String]$FontStyle = 'Normal',
[String]$FontWeight = 'Normal',
[Switch]$NewLine
)
$ParamOptions = $PSBoundParameters
$RichTextRange = New-Object System.Windows.Documents.TextRange( $RichTextBoxControl.Document.ContentEnd, $RichTextBoxControl.Document.ContentEnd )
if ($ParamOptions.ContainsKey('NewLine')) {
$RichTextRange.Text = "`n$Text"
}
else {
$RichTextRange.Text = $Text
}
$Defaults = @{ForeGroundColor='Black';BackGroundColor='White';FontSize='12'; FontStyle='Normal'; FontWeight='Normal'}
foreach ($Key in $Defaults.Keys) {
if ($ParamOptions.Keys -notcontains $Key) {
$ParamOptions.Add($Key, $Defaults[$Key])
}
}
$AllParameters = $ParamOptions.Keys | Where-Object {@('RichTextBoxControl','Text','NewLine') -notcontains $_}
foreach ($SelectedParam in $AllParameters) {
if ($SelectedParam -eq 'ForeGroundColor') {$TextElement = [System.Windows.Documents.TextElement]::ForegroundProperty}
elseif ($SelectedParam -eq 'BackGroundColor') {$TextElement = [System.Windows.Documents.TextElement]::BackgroundProperty}
elseif ($SelectedParam -eq 'FontSize') {$TextElement = [System.Windows.Documents.TextElement]::FontSizeProperty}
elseif ($SelectedParam -eq 'FontStyle') {$TextElement = [System.Windows.Documents.TextElement]::FontStyleProperty}
elseif ($SelectedParam -eq 'FontWeight') {$TextElement = [System.Windows.Documents.TextElement]::FontWeightProperty}
$RichTextRange.ApplyPropertyValue($TextElement, $ParamOptions[$SelectedParam])
}
}
$formstuffbox.Add_KeyDown( {
If ( $args[1].key -eq 'Return' ) {
$formLogReport.AppendText( "$( $formstuffbox.text )`n" )
}
} )
$formsubsitebox.Add_KeyDown( {
If ( $args[1].key -eq 'Return' ) {
Format-RichTextBox -RichTextBoxControl $formLogReport -Text $formsubsitebox.text -ForeGroundColor Red
}
} )
$form.ShowDialog()
有人知道更简单的方法吗?它只会用于错误,所以只需要变成红色。
【问题讨论】:
标签: wpf powershell xaml