【问题标题】:How can i display ComboboxSelectedItem in Textbox如何在文本框中显示组合框选定项
【发布时间】:2021-03-22 21:03:24
【问题描述】:

我使用 Visual Studio 创建了一个 GUI,并在 Powershell 中导入了 XAML 代码,但在文本框中显示来自 5 个组合框选定项之一的值时遇到问题。 我自己学习,我真的很喜欢它,但我没有单独调试代码的好方法, 任何人都可以有时间和知识来帮助我吗?

我想在文本框 ($TxtBox) 中显示来自 CboPL 或 CboTG 或 CboSB 的选定项目(或值)...。 使用 powershell。

这是我的 XAML 代码 beetwen here in powershell 中的字符串:

<Window x:Name="Main" x:Class="Printers_Ordi.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Printers_Ordi"
    mc:Ignorable="d"
    Title="Printers-Ordi" Height="450" Width="728.471">
<Grid x:Name="Wall">
    <Grid.Background>
        <ImageBrush ImageSource="C:/Users/admsb160365/Desktop/scripts Shell/Imprimantes GHT/Wall.png"/>
    </Grid.Background>
    <Label x:Name="LabelTitre" Content="Printers-Ordi" HorizontalAlignment="Left" Margin="74,25,0,0" VerticalAlignment="Top" FontFamily="Yu Gothic UI Semibold" FontSize="24" RenderTransformOrigin="0.503,0.76" Height="42" Width="164" Foreground="#FF1346CD"/>
    <ComboBox x:Name="CboComputer" HorizontalAlignment="Left" Margin="74,82,0,0" VerticalAlignment="Top" Width="164" FontSize="16" Height="31" IsEditable="True" IsReadOnly="True" Text="Ordinateur"/>
    <ComboBox x:Name="CboSites" HorizontalAlignment="Left" Margin="74,118,0,0" VerticalAlignment="Top" Width="164" Height="31" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Sites">
        <ComboBox x:Name="CboPL" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Paimpol"/>
        <ComboBox x:Name="CboGP" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Guingamp"/>
        <ComboBox x:Name="CboSB" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="SaintBrieuc"/>
        <ComboBox x:Name="CboLN" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Lannion"/>
        <ComboBox x:Name="CboTG" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Treguier"/>
    </ComboBox>

提前感谢您的耐心和支持 ;-)

【问题讨论】:

  • 您将使用条件逻辑来检查这些框中是否有文本,从这些组合框中收集/捕获 .text 值,并将该文本放置在表单元素的 .text 值中你需要它的地方。关于 WinForms/WPF 和 Combobox 用例,网络和 Youtube 上都有很多例子。

标签: powershell xaml combobox textbox display


【解决方案1】:

从我的 cmets 继续。有几种方法可以处理您的用例。这是一个使用 Winform 的简单示例。最终,它仍然从一个元素/或代码结果收集/分配文本到另一个。

Add-Type -AssemblyName System.Drawing

$ComboBox          = New-Object System.Windows.Forms.ComboBox
$ComboBox.Location = New-Object System.Drawing.Point(10,10)
$ComboBox.Items.AddRange(@('One','Two'))

$RichTextBox          = New-Object System.Windows.Forms.RichTextBox
$RichTextBox.Location = New-Object System.Drawing.Point(10,40)

$Form = New-Object System.Windows.Forms.Form

$Form.Controls.Add($ComboBox)
$Form.Controls.Add($RichTextBox)

$ComboBox.Add_TextChanged({
    switch($ComboBox.Text){
    'One' {$RichTextBox.Text = 'This is one'}
    'Two' {$RichTextBox.Text = 'This is two'}
    }
})

$Form.ShowDialog()

使用 WPF 的示例

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')

[xml]$XAML = @'
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="PowerShell Form" Height="310" Width="350" ResizeMode="NoResize">
    <Grid Margin="0,0,0,0">
        <GroupBox Header="General" HorizontalAlignment="Left" Height="225" Margin="10,10,0,0" VerticalAlignment="Top" Width="320">
            <Grid HorizontalAlignment="Left" Height="206" Margin="0,0,0,0" VerticalAlignment="Top" Width="320">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60*"/>
                    <ColumnDefinition Width="250*"/>
                </Grid.ColumnDefinitions>
                <Label Content="Field #1:" HorizontalAlignment="Left" Margin="0,7,0,0" VerticalAlignment="Top" Height="26" Width="93"/>
                <Label Content="Field #2:" HorizontalAlignment="Left" Margin="0,35,0,0" VerticalAlignment="Top" Height="26" Width="93"/>
                <Label Content="Field #3:" HorizontalAlignment="Left" Margin="0,65,0,0" VerticalAlignment="Top" Height="26" Width="93"/>
                <TextBox Name="f1" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="229" Grid.Column="1"/>
                <ComboBox Name="f2" HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" VerticalAlignment="Top" Width="229" Grid.Column="1">
                    <ListBoxItem>Test 1</ListBoxItem>
                    <ListBoxItem>Test 2</ListBoxItem>
                    <ListBoxItem>Test 3</ListBoxItem>
                </ComboBox>
                <TextBox Name="f3" HorizontalAlignment="Left" Height="23" Margin="10,66,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="229" Grid.Column="1"/>
            </Grid>
        </GroupBox>
        <Button Name="Clear" Content="Clear Form" HorizontalAlignment="Left" Margin="10,240,0,0" VerticalAlignment="Top" Width="90" Height="30" RenderTransformOrigin="0.522,0.733"/>
        <Button Name="Submit" Content="Submit" Margin="234,240,12,0" VerticalAlignment="Top" Height="30"/>
    </Grid>
</Window>
'@

$reader   = (New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form = [Windows.Markup.XamlReader]::Load( $reader )}
catch
{
    Write-Host 'Unable to load Windows.Markup.XamlReader.'
    break
}

$xaml.SelectNodes('//*[@Name]') | 
ForEach-Object {Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}

$Form.Add_Loaded({    
    if (Test-Path -Path '.\ComboBox.selection') 
    {
        $cbSelection = Get-Content '.\ComboBox.selection'
        [int]$cbSelectionInt = -1
        [void][int]::TryParse($cbSelection,[ref]$cbSelectionInt)

        if ($cbSelectionInt -ge 0) 
        {$f2.SelectedIndex = $cbSelectionInt}
    }
})


$Clear.Add_Click({
    $fields = @('f1','f2','f3')

    foreach ($field in $fields) 
    {
        $control = $Form.FindName($field)
        switch ($control.GetType()) {
            'System.Windows.Controls.TextBox' {$control.Clear()}
            'System.Windows.Controls.ComboBox' {$control.SelectedIndex = -1}
            DEFAULT {'unknown control'}
        }
    }
})

$f2.add_SelectionChanged({
    param($sender,$args)
    $f3.Text = ($($sender.SelectedValue) -replace '.*:\s+')
})

$Form.ShowDialog()

【讨论】:

  • 您好,感谢您的解释,但我不知道如何使用我的变量调整这部分代码:$ComboBox.Add_TextChanged({ switch($ComboBox.Text){ 'One' {$RichTextBox.Text = '这是一个'} '二' {$RichTextBox.Text = '这是两个'} } })
  • 在这种情况下,例如我想从 $WPFCboPL 中选择选定的值并在 $WPFTxtBox 中显示这个值。
  • 了解,但这不会改变所提供的方法。您只需提供适当的表单元素来共享文本数据。您没有展示足够多的代码来了解您为实现目标而正在或未在做什么。您只显示表单元素的 XAML。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多