PowerShell\WPF:PowerShell 中使用数据模板的空单选按钮对象
PowerShell\WPF: Emtpy Radio Button Object in PowerShell using datatemplate
我目前正在使用单选按钮和文本块的数据模板,这些模板由从 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 的一个片段。如上所述,显示有效,PowerShell 中的对象为 null 并且没有显示为单选按钮,这是我的问题。
我通过将 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>
我目前正在使用单选按钮和文本块的数据模板,这些模板由从 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 的一个片段。如上所述,显示有效,PowerShell 中的对象为 null 并且没有显示为单选按钮,这是我的问题。
我通过将 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>