将事件分配给通过Datatemplate创建的WPF控件
使用powershell,我有一个WPF treeview控件,其中我使用了一个数据模板来填充。以下是一个例子: 我当前的问题是,我无法为通过DATATEMPLATE创建的控件注册单击事件。实际上,$window.Findname找不到该控件,如果它是通过datatemplate创建的 如果我通过编辑XAML代码或在运行时创建控件来添加一个简单的按钮,FINDNAME或附加到控件的变量就可以很好地工作。我可以注册点击事件 但是,如果控件是通过datatemplate创建的,FINDNAME根本不起作用 有什么建议吗 PS:以下是嵌入的powershell代码WPF:将事件分配给通过Datatemplate创建的WPF控件,wpf,powershell,hierarchicaldatatemplate,Wpf,Powershell,Hierarchicaldatatemplate,使用powershell,我有一个WPF treeview控件,其中我使用了一个数据模板来填充。以下是一个例子: 我当前的问题是,我无法为通过DATATEMPLATE创建的控件注册单击事件。实际上,$window.Findname找不到该控件,如果它是通过datatemplate创建的 如果我通过编辑XAML代码或在运行时创建控件来添加一个简单的按钮,FINDNAME或附加到控件的变量就可以很好地工作。我可以注册点击事件 但是,如果控件是通过datatemplate创建的,FINDNAME根本
Cls
Add-Type -Assembly PresentationFramework
Add-Type -Assembly PresentationCore
Function btnReload_click
{
Write-Host $this.name
}
$btnReload_click=({
$MyItemsListProperty = @(New-Object PSObject -Property @{Title='New guy in the town';icon="C:\temp\computer.png";Reloadbtn="Visible";spLoading="Hidden"})
$myDATA.Add($MyItemsListProperty)
$treeView.ItemsSource = $myDATA
})
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Threaded WPF Explorer" Height="840" Width="350" Icon="C:\temp\Computer.png" WindowStartupLocation="CenterScreen" >
<Grid>
<TreeView x:Name="foldersTree">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate DataType="ContentPresenter">
<Grid>
<StackPanel Name="spImg" Orientation="Horizontal">
<Image Name="img"
Source="{Binding icon}"
Width="20" Height="20" Stretch="Fill" VerticalAlignment="Center" />
<TextBlock Text="{Binding Title}" Margin="5,0" VerticalAlignment="Center" />
<Button x:Name="btnReload"
Visibility="{Binding Reloadbtn}"
Height="14" VerticalAlignment="Center"
Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}"
>
<TextBlock FontSize="9" Text="Reload..." TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
</Button>
<StackPanel Name="spLoading" Orientation="Horizontal"
Visibility="{Binding spLoading}"
>
<Grid Height="13" MinWidth="50" MaxWidth="75" Margin="5,0" >
<ProgressBar Name="pbLoading"
Height="13"
MinWidth="50"
MaxWidth="75"
IsIndeterminate="True"
HorizontalAlignment="Center"
VerticalAlignment="Center">
</ProgressBar>
<TextBlock Name="txtLoading" Text="Loading..." FontSize="8.6" Margin="5,0" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Red"/>
</Grid>
<Button Name="btnCancelLoad"
IsEnabled="$True"
Height="14" VerticalAlignment="Center"
Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}"
>
<TextBlock FontSize="9" Text="Cancel" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
</Button>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
<Button Name="btnTest" Height="25" VerticalAlignment="Bottom" IsEnabled="$True">Add Item</Button>
</Grid>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$treeView=$Window.FindName("foldersTree")
$tstBTN=$Window.FindName("btnTest")
$treeview.add_SelectedItemChanged({
$sender = $args[0]
Write-Host $this.selecteditem.Title
})
$tstBTN.add_Click($btnReload_click)
$myDATA=New-Object System.Collections.ObjectModel.ObservableCollection[object]
$MyItemsListProperty = @(New-Object PSObject -Property @{Title='Image with computer';icon="C:\temp\computer.png";Reloadbtn="Visible";spLoading="Hidden"})
$myDATA.Add($MyItemsListProperty)
$MyItemsListProperty = @(New-Object PSObject -Property @{Title='Image with folder';icon="C:\temp\folder.png";Reloadbtn="Visible";spLoading="Hidden"})
$myDATA.Add($MyItemsListProperty)
$MyItemsListProperty = @(New-Object PSObject -Property @{Title='Image with diskdrive';icon="C:\temp\diskdrive.png";Reloadbtn="Visible";spLoading="Hidden"})
$myDATA.Add($MyItemsListProperty)
#$myWindow_btnReload.Add_Click($btnReload_click)
$treeView.ItemsSource = $myDATA
$Window.ShowDialog()|Out-Null
Cls
添加类型-程序集PresentationFramework
添加类型-程序集表示核心
功能btnReload\u单击
{
将主机$this.name写入
}
$btnReload\u单击=({
$MyItemsListProperty=@(新对象PSObject-Property@{Title='New guy in the town';icon=“C:\temp\computer.png”;Reloadbtn=“Visible”;spLoading=“Hidden”})
$myDATA.Add($MyItemsListProperty)
$treeView.ItemsSource=$myDATA
})
[xml]$xaml=@”
添加项
"@
$reader=(新对象System.Xml.XmlNodeReader$xaml)
$Window=[Windows.Markup.XamlReader]::加载($reader)
$treeView=$Window.FindName(“foldersTree”)
$tstBTN=$Window.FindName(“btnTest”)
$treeview.add\u SelectedItemChanged({
$sender=$args[0]
写入主机$this.selecteditem.Title
})
$tstBTN.add_Click($btnReload_Click)
$myDATA=New Object System.Collections.ObjectModel.ObservableCollection[Object]
$MyItemsListProperty=@(新对象PSObject-Property@{Title='Image with computer';icon=“C:\temp\computer.png”;Reloadbtn=“Visible”;spLoading=“Hidden”})
$myDATA.Add($MyItemsListProperty)
$MyItemsListProperty=@(新对象PSObject-Property@{Title='Image with folder';icon=“C:\temp\folder.png”;Reloadbtn=“Visible”;spLoading=“Hidden”})
$myDATA.Add($MyItemsListProperty)
$MyItemsListProperty=@(新对象PSObject-Property@{Title='Image with diskdrive';icon=“C:\temp\diskdrive.png”;Reloadbtn=“Visible”;spLoading=“Hidden”})
$myDATA.Add($MyItemsListProperty)
#$myWindow\u btnReload.Add\u Click($btnReload\u Click)
$treeView.ItemsSource=$myDATA
$Window.ShowDialog()| Out Null
您能显示一些代码吗?您是否尝试过eventtriggers
(在同一个数据模板中)?通过触发器或命令?我已将powershell代码添加到主帖子中。