WPF/PowerShell列表框:如果项与*表达式匹配,则更改其前景色*

WPF/PowerShell列表框:如果项与*表达式匹配,则更改其前景色*,wpf,xaml,powershell,Wpf,Xaml,Powershell,我试图根据列表框中项目的内容更改其前景色 <ListBox x:Name="listBox_advList" HorizontalAlignment="Left" Height="277" Margin="10,38,0,0" VerticalAlignment="Top" Width="491"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxI

我试图根据列表框中项目的内容更改其前景色

<ListBox x:Name="listBox_advList" HorizontalAlignment="Left" Height="277" Margin="10,38,0,0" VerticalAlignment="Top" Width="491">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Style.Triggers>
                    <Trigger Property="Content" Value="GIE">
                        <Setter Property="ListBoxItem.Background" Value="LightGreen" />
                    </Trigger>
                    <Trigger Property="Content" Value="Console Admin Tools">
                        <Setter Property="ListBoxItem.Background" Value="Orange" />
                    </Trigger>                               
                </Style.Triggers>
            </Style>
        </ListBox.ItemContainerStyle>
        </ListBox>

我想做的是根据$value的值更改每个项目的颜色。\u State。如果包含成功,则列表框中的项应为绿色,如果包含失败,则应为橙色/红色。。。谢谢!:)

您可以在样式加载到PowerShell后对其进行操作。您只需找到ListBox控件,然后循环遍历这些项,并相应地格式化它们。假设此控件的父控件已分配给变量
$Form
,则可以执行以下操作:

$ListBox = $Form.FindName('listBox_advList')
Switch($ListBox.Items){
    {$_.Content -match 'success'} {$_.Background = 'Green'}
    {$_.Content -match 'failure'} {$_.Background = 'Orange'}
}

嗯,我个人不会去触发器处理这种行为。在这种情况下,我会利用IValueConverter。使用此选项,我们可以获取绑定值,将其发送到转换器,并返回要使用的笔刷颜色

例如,我想将等于“SUCCESS”的字符串值转换为返回绿色画笔,并将任何其他值转换为返回红色画笔。这就是我要做的:

<Window x:Class="WpfApp1.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:WpfApp1"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<!-- Declare my converter as a resource in my window -->
<Window.Resources>
    <local:StringToBrushConverter x:Key="colorConverter"></local:StringToBrushConverter>
</Window.Resources>

<!-- My window Content -->
<Grid>
    <ListBox>
        <!-- String items for listbox (This could be a binded collection of strings) -->
        <ListBox.Items>
            <sys:String>SUCCESS</sys:String>
            <sys:String>SUCCESS</sys:String>
            <sys:String>FAILURE</sys:String>
            <sys:String>SUCCESS</sys:String>
            <sys:String>FAILURE</sys:String>
        </ListBox.Items>

        <!-- Set the item template of the items, to a text block -->
        <ListBox.ItemTemplate>
            <DataTemplate>
                <!-- Bind 'Text' to the string value, bind the foreground to the returned converter value given the string -->
                <TextBlock Text="{Binding}" Foreground="{Binding Converter={StaticResource colorConverter}}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

我认为这是一种很好的方法,在我看来比使用触发器容易得多。

我最终使用了这种方法,正如technet上建议的那样:

添加类型-AssemblyName presentationframework
[xml]$xaml=@'
'@
$reader=(新对象System.Xml.XmlNodeReader$xaml)
$Window=[Windows.Markup.XamlReader]::加载($reader)
$ListBox=$Window.FindName(“ListBox”)
$itm=新对象System.Windows.Controls.ListboxItem
$itm.Content='test red'
$itm.前台=‘红色’
$ListBox.Items.Add($itm)
$itm=新对象System.Windows.Controls.ListboxItem
$itm.Content='test green'
$itm.前台=‘绿色’
$ListBox.Items.Add($itm)
$itm=新对象System.Windows.Controls.ListboxItem
$itm.Content='test blue'
$itm.前台=‘蓝色’
$ListBox.Items.Add($itm)
$Window.ShowDialog()

很好,再次感谢各位

是否通过XmlNodeReader/XamlReader将此XML转换为PowerShell对象?(如所解释的内容)是的,这正是我正在做的。+1用于使用switch语句,因为您可以轻松地向其添加更多条件。作为奖励,您可以运行一个
Foreach($collection中的item)
循环,然后调用switch语句。我理解您试图做什么,但我不知道如何根据该示例填充我的列表框。现在我正在使用这样的ForEach对象:$sortAdv | ForEach对象{$listBox\u advList.AddText($\uu.PKG\u Name)}向listBox添加内容。此外,后台属性只能设置为整个listBox,而不能按项目设置:没有$listBox.items.content和$listBox.items.Background。也许我没理解你的意思,我是新手。有什么提示吗?真烦人……:)对不起,直到今天我才看到你的评论
$listbox.items
ListBoxItem
对象的集合。每个都有
背景
内容
属性,您可以在最终使用的代码中看到这些属性。谢谢,但我需要在PowerShell中执行此操作。
<Window x:Class="WpfApp1.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:WpfApp1"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<!-- Declare my converter as a resource in my window -->
<Window.Resources>
    <local:StringToBrushConverter x:Key="colorConverter"></local:StringToBrushConverter>
</Window.Resources>

<!-- My window Content -->
<Grid>
    <ListBox>
        <!-- String items for listbox (This could be a binded collection of strings) -->
        <ListBox.Items>
            <sys:String>SUCCESS</sys:String>
            <sys:String>SUCCESS</sys:String>
            <sys:String>FAILURE</sys:String>
            <sys:String>SUCCESS</sys:String>
            <sys:String>FAILURE</sys:String>
        </ListBox.Items>

        <!-- Set the item template of the items, to a text block -->
        <ListBox.ItemTemplate>
            <DataTemplate>
                <!-- Bind 'Text' to the string value, bind the foreground to the returned converter value given the string -->
                <TextBlock Text="{Binding}" Foreground="{Binding Converter={StaticResource colorConverter}}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
public class StringToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((string)value == "SUCCESS") //Return green brush (P.S. Case sensitive)
        {
            return Brushes.Green;
        }
        else if ((string)value == "FOOBAR") //If you ever wanted other color options
        {
            return Brushes.Yellow;
        }

        //Anything not caught will be red. AKA "FAILURE" would belong here.
        return Brushes.Red;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
 Add-Type -AssemblyName presentationframework
[xml]$xaml = @'
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window" Title="Progress..." WindowStartupLocation = "CenterScreen"
    Width = "400" Height = "165" ShowInTaskbar = "True">
    <Grid>
        <ListBox x:Name="ListBox" Height = "150" Width = "365" HorizontalAlignment="Left" VerticalAlignment="Top"  Margin = "10,35,0,0"/>
    </Grid>
</Window>
'@

$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$Window = [Windows.Markup.XamlReader]::Load($reader)

$ListBox = $Window.FindName("ListBox")

$itm = new-object System.Windows.Controls.ListboxItem
$itm.Content = 'test red'
$itm.Foreground = 'red'
$ListBox.Items.Add($itm)

$itm = new-object System.Windows.Controls.ListboxItem
$itm.Content = 'test green'
$itm.Foreground = 'green'
$ListBox.Items.Add($itm)

$itm = new-object System.Windows.Controls.ListboxItem
$itm.Content = 'test blue'
$itm.Foreground = 'blue'
$ListBox.Items.Add($itm)

$Window.ShowDialog()