PowerShell\WPF:使用数据模板的 PowerShell 中的空单选按钮对象

Posted

技术标签:

【中文标题】PowerShell\\WPF:使用数据模板的 PowerShell 中的空单选按钮对象【英文标题】:PowerShell\WPF: Emtpy Radio Button Object in PowerShell using datatemplatePowerShell\WPF:使用数据模板的 PowerShell 中的空单选按钮对象 【发布时间】:2022-01-21 18:04:42 【问题描述】:

我目前正在使用单选按钮和文本块的数据模板,这些模板由从 Excel 电子表格中提取的值自动生成。但是它可以工作,我遇到了一个问题,即 RadioButton 对象出现在我的变量 $form(wpf items) 数组中,但它的值为 null。

这是我的 WPF。如果您想查看 PowerShell 代码,我也可以附上。

<Page x:Name="templateMenu" 
  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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" 
  mc:Ignorable="d"
    Title="templateMenu" 
    Height="450" Width="400"
    Background="Transparent">

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.TextBlock.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ToggleButton.xaml"/>
            <ResourceDictionary Source="C:\Users\Tre\Documents\repo\boip creation tool\Utils\CustomToolBar.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>

<Border Background="#212C3E"
        CornerRadius="20">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="91*"/>
            <RowDefinition Height="248*"/>
            <RowDefinition Height="61*"/>
        </Grid.RowDefinitions>

        <ToolBarTray VerticalAlignment="Top" HorizontalAlignment="Right" Height="50" Width="76" Grid.Column="1" Background="Transparent">
            <ToolBar x:Name="templateMenuToolBar">

                <Button x:Name="templateMenuBtnClose" Style="StaticResource MaterialDesignIconButton"
                        Content= "materialDesign:PackIcon Kind=Close"
                        Foreground="White"
                        materialDesign:RippleAssist.Feedback="#DD000000"
                        IsEnabled="Binding DataContext.ControlsEnabled, RelativeSource=RelativeSource FindAncestor, AncestorType=Window" IsCancel="True"/>

            </ToolBar>
        </ToolBarTray>

        <TextBlock x:Name="templateMenuTitleOne" HorizontalAlignment="Center"
                   VerticalAlignment="Center"
                   Text="Select Template to Create BOIPS" 
                   TextWrapping="Wrap"
                   Grid.ColumnSpan="2"
                   FontSize="24"
                   Foreground="#FFFFD960" 
                   Width="260"
                   TextAlignment="Center" 
                   Height="64"  
                   Grid.Row="1" Margin="70,14,70,13"/>

       <StackPanel x:Name="tempMenuPanel" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Height="auto">
            <Border BorderThickness="1" BorderBrush="Transparent" Background="White" CornerRadius="10" Height="auto" Margin="8,5,8,5">
            
                    <ItemsControl x:Name="templateOptions"
                          ItemsSource="Binding templateOptions"
                          Grid.IsSharedSizeScope="True"
                          Height="238" Width="383">
                     

                    <ItemsControl.ItemTemplate>
                            <DataTemplate x:Key="RadioBtnTemplate">
                                <Border x:Name="Border"
                                Padding="8">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition
                                             SharedSizeGroup="Checkerz" />
                                            <ColumnDefinition />
                                        </Grid.ColumnDefinitions>
                                         <RadioButton 
                                            VerticalAlignment="Center"
                                            Foreground="#FF2095F2" 
                                            GroupName="Main">
                                            <RadioButton.Resources>
                                                <!--Unchecked state-->
                                                <SolidColorBrush x:Key="MaterialDesignCheckBoxOff" Color="#FF2095F2"/>
                                                <!--Checked state-->
                                                <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#FF2095F2"/>
                                            </RadioButton.Resources>
                                        </RadioButton>
                                        <StackPanel
                                        Margin="8 0 0 0"
                                        Grid.Column="1">
                                            <TextBlock
                                            Text="Binding Template Type"
                                            FontWeight="Bold"
                                            Foreground="#2095F2" />
                                            <TextBlock
                                            Text="Binding Description"
                                            Foreground="black"
                                            TextWrapping="Wrap" />
                                        </StackPanel>
                                    </Grid>
                                
                            </Border>
                            <DataTemplate.Triggers>
                                <DataTrigger
                                  Binding="Binding IsSelected"
                                  Value="True">
                                    <Setter
                                    TargetName="Border"
                                    Property="Background"
                                    Value="DynamicResource MaterialDesignSelection" />
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.Template>
                    <ControlTemplate TargetType="ItemsControl">
                        <ScrollViewer VerticalScrollBarVisibility="Auto">
                            <ItemsPresenter Margin="5" />
                        </ScrollViewer>
                    </ControlTemplate>
                </ItemsControl.Template>
                </ItemsControl>
            </Border>
        </StackPanel>
        <Button x:Name="BtnBacktemplateMenu" Content="Back" 
                    HorizontalAlignment="Center" 
                    Grid.Row="3" 
                    VerticalAlignment="Center"
                    Foreground="#2095F2"
                    Height="32" Width="130"
                    Style="DynamicResource MaterialDesignRaisedLightButton" Background="#FF18202D" BorderBrush="Transparent" FontWeight="Normal" Margin="35,13,35,16"/>

        <Button x:Name="BtnNexttemplateMenu" Content="Next" 
                    HorizontalAlignment="Center" 
                    Grid.Row="3"
                    Grid.Column="1"
                    VerticalAlignment="Center"
                    Foreground="#FFFFD960"
                    Height="32" Width="130"
                    Style="DynamicResource MaterialDesignRaisedLightButton" Background="#FF18202D" BorderBrush="Transparent" FontWeight="Normal" Margin="35,13,35,16" IsEnabled="False"/>
    </Grid>
</Border>

这是 GUI 的 sn-p。就像上面提到的那样,显示工作正常,PowerShell 中的对象是 null 并且不显示为单选按钮,这是我的问题。

回购:https://github.com/creationsoftre/wpf-datatemp-test

【问题讨论】:

【参考方案1】:

我通过将 itemsControl 更改为使用 selectedValue、SelectedIndex 来控制自动生成的单选按钮的列表框来解决我的问题。

PowerShell 代码

$wpf.menuListBox.add_SelectionChanged(
    $wpf.BtnNexttemplateMenu.IsEnabled = $true
    
    if($wpf.menuListBox.SelectedIndex -lt $wpf.menuListBox.Items.Count)
        $selectedItem = $wpf.menuListBox.SelectedValue.'Template Type'
    
)

Xaml 代码

<ListBox x:Name="menuListBox"
        ItemsSource="Binding dataContent"
        Grid.IsSharedSizeScope="True"
        Height="238" Width="383">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border x:Name="Border"
            Padding="8">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition
                         SharedSizeGroup="Checkerz" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <RadioButton 
                        VerticalAlignment="Center"
                        Foreground="#FF2095F2" 
                        GroupName="Templates"
                        IsChecked="Binding RelativeSource=RelativeSource AncestorType=x:Type ListBoxItem, Path=IsSelected">
                        <RadioButton.Resources>
                            <!--Unchecked state-->
                            <SolidColorBrush x:Key="MaterialDesignCheckBoxOff" Color="#FF2095F2"/>
                            <!--Checked state-->
                            <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#FF2095F2"/>
                        </RadioButton.Resources>
                    </RadioButton>
                    <StackPanel
                    Margin="8 0 0 0"
                    Grid.Column="1">
                        <TextBlock
                        Text="Binding Template Type"
                        FontWeight="Bold"
                        Foreground="#2095F2" />
                        <TextBlock
                        Text="Binding Description"
                        Foreground="black"
                        TextWrapping="Wrap" />
                    </StackPanel>
                </Grid> 
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger
                  Binding="Binding IsSelected"
                  Value="True">
                    <Setter
                    TargetName="Border"
                    Property="Background"
                    Value="DynamicResource MaterialDesignSelection" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.Template>
        <ControlTemplate TargetType="ItemsControl">
            <ScrollViewer VerticalScrollBarVisibility="Auto">
                <ItemsPresenter Margin="5" />
            </ScrollViewer>
        </ControlTemplate>
    </ListBox.Template>
</ListBox>

【讨论】:

以上是关于PowerShell\WPF:使用数据模板的 PowerShell 中的空单选按钮对象的主要内容,如果未能解决你的问题,请参考以下文章

powershell WPF与PowerShell

powershell WPF ClickOnce应用程序的Octopus部署脚本示例。

Hibernate的save方法不能进行数据库插入

odoo国际化翻译

关于操作系统中英文切换的.po和.mo介绍

[原创]java向word模板中填充数据(总结)