将 Click 事件添加到 PowerShell 中的 WPF DataGrid 按钮列 [英] Add Click event to WPF DataGrid Button Column in PowerShell
问题描述
通常我用于带有按钮的数据网格列的 XAML 是这样的:
Normally the XAML I use for a data grid column with a button is something like this:
<DataGridTemplateColumn Header="ButtonColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Click_Handler">Do Something</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
在 Powershell 中使用 XAML 时,但据我所知无法使用 Click=,因为它会引发错误:
When using XAML in Powershell however Click= can't be used as far as I know, as it throws error:
"Failed to create a 'Click' from the text 'ClickEvent'.
如何在运行时获取对按钮对象的引用,以添加单击事件处理程序?或者有没有办法在 Powershell 中创建一个可以由 XAML 事件处理程序中指定的名称触发的函数.
How can I get a reference to the button object at run time, to add the click event handlers? Or is there a way to create a function in Powershell that can be triggered by the name specified in XAML event handler.
为对象命名并使用
$window.FindName("<button name>")
不起作用
正在构建的 GUI 的简化示例:
A simplified example of the GUI being built:
[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="Demo" WindowStartupLocation = "CenterScreen"
SizeToContent="WidthAndHeight"
ShowInTaskbar = "True">
<StackPanel Orientation="Vertical" Name="msiStackPanel">
<TextBlock Margin="20,20,20,20" HorizontalAlignment="Center" FontSize="14" FontWeight="Bold">List of Stuff</TextBlock>
<DataGrid Name="myGrid" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTemplateColumn Header="Launch">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="x:launchButton">Launch</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button Margin="20,20,20,20">Completed All Tests</Button>
</StackPanel>
</Window>
'@
# Set PowerShell variable for each named object in GUI
Function Set-WpfVariables
{
param($children)
ForEach ($child in $children.Children)
{
Set-WpfVariables -Children $child
if (![String]::IsNullOrEmpty($child.Name))
{
Write-Host "Set variable $($child.Name)"
Set-Variable -Name $child.Name -Value $child -Scope global
}
}
}
# Build Window
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$controls = [System.Windows.LogicalTreeHelper]::GetChildren($window)
Set-WpfVariables -Children $controls
$table = New-Object System.Data.DataTable
$table.Columns.Add("Name")
$table.Rows.Add("Row 1")
$table.Rows.Add("Row 2")
$myGrid.DataContext = $table.DefaultView
$window.ShowDialog()
推荐答案
以下是任何感兴趣的人的工作脚本:
Here's a working script for anybody interested:
[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="Demo" WindowStartupLocation = "CenterScreen"
SizeToContent="WidthAndHeight"
ShowInTaskbar = "True">
<StackPanel Orientation="Vertical" Name="msiStackPanel">
<TextBlock Margin="20,20,20,20" HorizontalAlignment="Center" FontSize="14" FontWeight="Bold">List of Stuff</TextBlock>
<DataGrid Name="myGrid" ItemsSource="{Binding}">
<DataGrid.Columns>
</DataGrid.Columns>
</DataGrid>
<Button Margin="20,20,20,20">Completed All Tests</Button>
</StackPanel>
</Window>
'@
# Set PowerShell variable for each named object in GUI
Function Set-WpfVariables
{
param($children)
ForEach ($child in $children.Children)
{
Set-WpfVariables -Children $child
if (![String]::IsNullOrEmpty($child.Name))
{
Write-Host "Set variable $($child.Name)"
Set-Variable -Name $child.Name -Value $child -Scope global
}
}
}
# Build Window
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$controls = [System.Windows.LogicalTreeHelper]::GetChildren($window)
Set-WpfVariables -Children $controls
$table = New-Object System.Data.DataTable
$table.Columns.Add("Name")
$table.Rows.Add("Row 1")
$table.Rows.Add("Row 2")
$table.Rows.Add("Row 3")
#$myGrid.DataContext = $table.DefaultView #This did not work for me. so...
$myGrid = $window.FindName("myGrid")
$myGrid.ItemsSource = $table.DefaultView
[System.Windows.RoutedEventHandler]$clickEvent = {
param ($sender,$e)
Write-Host "Clicked row $($myGrid.SelectedItem.Row.Name)"
}
$buttonColumn = New-Object System.Windows.Controls.DataGridTemplateColumn
$buttonFactory = New-Object System.Windows.FrameworkElementFactory([System.Windows.Controls.Button])
$buttonFactory.SetValue([System.Windows.Controls.Button]::ContentProperty, "Launch")
$buttonFactory.AddHandler([System.Windows.Controls.Button]::ClickEvent,$clickEvent)
$dataTemplate = New-Object System.Windows.DataTemplate
$dataTemplate.VisualTree = $buttonFactory
$buttonColumn.CellTemplate = $dataTemplate
$myGrid.Columns.Add($buttonColumn)
$myGrid.DataContext = $table.DefaultView
$window.ShowDialog()
这篇关于将 Click 事件添加到 PowerShell 中的 WPF DataGrid 按钮列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!