将事件分配给通过Datatemplate创建的WPF控件

时间:2017-07-24 10:37:07

标签: wpf powershell hierarchicaldatatemplate

使用PowerShell,我有一个WPF树视图控件,我使用数据模板填充。这是一个例子:

https://i.stack.imgur.com/FFMQN.png

我目前的问题是我无法为通过DATATEMPLATE创建的控件注册CLICK事件。实际上$ window.Findname找不到控件,如果它是通过datatemplate创建的。

如果我通过编辑XAML代码添加一个简单的按钮或在运行时创建控件,那么FINDNAME或附加到控件的变量效果很好。我可以注册点击事件。

但是如果通过datatemplate创建控件,则FINDNAME根本不起作用。

有什么建议吗?

PS:这是PowerShell代码WPF嵌入式:

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

0 个答案:

没有答案