gpt4 book ai didi

wpf - 将Click事件添加到PowerShell中的WPF DataGrid按钮列中

转载 作者:行者123 更新时间:2023-12-03 01:09:12 25 4
gpt4 key购买 nike

通常,我用于带有按钮的数据网格列的XAML是这样的:

<DataGridTemplateColumn Header="ButtonColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="Click_Handler">Do Something</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

据我所知,在Powershell中使用XAML时,Click =不能使用,因为它会引发错误:
 "Failed to create a 'Click' from the text 'ClickEvent'.

如何在运行时获取对按钮对象的引用,以添加click事件处理程序?还是有一种方法可以在Powershell中创建可以由XAML事件处理程序中指定的名称触发的函数。

给对象命名并使用
$window.FindName("<button name>")

不起作用

正在构建的GUI的简化示例:
 [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()

最佳答案

这是任何感兴趣的人的工作脚本:

[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()

关于wpf - 将Click事件添加到PowerShell中的WPF DataGrid按钮列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42288974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com