Wpf 这是一个关于追踪专栏的好主意。再调整几次,我就可以完成完整的Find函数了。感谢您的时间和专业知识。 [xml]$xaml=@" <Window xmlns="http://schemas.microsoft.com/winfx/2006/x
这是一个关于追踪专栏的好主意。再调整几次,我就可以完成完整的Find函数了。感谢您的时间和专业知识。Wpf 这是一个关于追踪专栏的好主意。再调整几次,我就可以完成完整的Find函数了。感谢您的时间和专业知识。 [xml]$xaml=@" <Window xmlns="http://schemas.microsoft.com/winfx/2006/x,wpf,powershell,datagrid,Wpf,Powershell,Datagrid,这是一个关于追踪专栏的好主意。再调整几次,我就可以完成完整的Find函数了。感谢您的时间和专业知识。 [xml]$xaml=@" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/exp
[xml]$xaml=@"
<Window
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:local="clr-namespace:Test"
Title="MainWindow" Height="175" Width="550">
<Grid>
<TextBox x:Name="tb_Search" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="149"/>
<Button x:Name="bt_Search" Content="Search" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" IsDefault="True" Height="22" Margin="165,10,0,0" />
<DataGrid x:Name="dg" Margin="10,45,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="100" Height="Auto" Width="Auto" ColumnWidth="Auto" AlternationCount="1" IsReadOnly="True" SelectionMode="Extended" SelectionUnit="Cell" Background="White" />
</Grid>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load($reader)
#Turn XAML into PowerShell objects
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'x:Name')]]") | ForEach-Object{
Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)
}
#sample data
$DataSet = New-Object System.Data.DataSet
$Table = $DataSet.Tables.Add("Table")
$Properties = @("Country","Capital","Population")
$Properties | foreach {
$Column = New-Object System.Data.DataColumn($_)
$Table.Columns.Add($Column)
}
$Null=$Table.Rows.Add("China PR","Beijing","20,693,000")
$Null=$Table.Rows.Add("India","New Delhi","16,787,949")
$Null=$Table.Rows.Add("Japan","Tokyo","13,189,000")
$Null=$Table.Rows.Add("Philippines","Manila","12,877,253")
$Null=$Table.Rows.Add("Russia","Moscow","11,541,000")
$Null=$Table.Rows.Add("Egypt","Cairo","10,230,350")
$Null=$Table.Rows.Add("USA","Washington, D.C","658,893")
$Null=$Table.Rows.Add("China PR","Beijing","20,693,000")
$Null=$Table.Rows.Add("India","New Delhi","16,787,949")
$Null=$Table.Rows.Add("Japan","Tokyo","13,189,000")
$Null=$Table.Rows.Add("Philippines","Manila","12,877,253")
$Null=$Table.Rows.Add("Russia","Moscow","11,541,000")
$Null=$Table.Rows.Add("Egypt","Cairo","10,230,350")
$Null=$Table.Rows.Add("USA","Washington, D.C","658,893")
#populate datagrid
$DataView = New-Object System.Data.DataView($Table)
$array = New-Object System.Collections.ArrayList
[void] $array.AddRange($DataView)
$dg.clear()
$dg.ItemsSource = $array
$dg.IsReadOnly = $true
$bt_Search.Add_Click({
$SearchValue = $tb_Search.text
for ($i = 0; $i -lt $dg.Items.Count; $i++)
{
if ($dg.Items[$i].Row[$dg.Columns.DisplayIndex] -eq "$SearchValue")
{
[System.Windows.Forms.MessageBox]::Show("Keyword Found")
$dg.ScrollIntoView($dg.Items[$i]) #scroll to the row that contains the keyword searched
}
}
})
#Display Form
$Window.ShowDialog() | Out-Null
$bt_Search.Add_Click({
$SearchValue = $tb_Search.text
$dg.SelectedCells.Clear()
for ($i = 0; $i -lt $dg.Items.Count; $i++)
{
0..($dg.Columns.Count-1)|?{$dg.Items[$i].Row[$_] -eq "$SearchValue"}|%{
$dg.ScrollIntoView($dg.Items[$i],$dg.Columns[$_])
$DGCell = $dg.Columns[$_].GetCellContent($dg.Items[$i]).Parent
$DGCellInfo = New-Object System.Windows.Controls.DataGridCellInfo($DGCell)
$dg.SelectedCells.add($DGCellInfo)
}
}
})
$global:SearchIndex = 0
$bt_Search.Add_Click({
$SearchValue = $tb_Search.text
$dg.SelectedCells.Clear()
for ($i = $global:SearchIndex; $i -lt $dg.Items.Count; $i++)
{
0..($dg.Columns.Count-1)|?{$dg.Items[$i].Row[$_] -eq "$SearchValue"}|%{
$dg.ScrollIntoView($dg.Items[$i],$dg.Columns[$_])
$DGCell = $dg.Columns[$_].GetCellContent($dg.Items[$i]).Parent
$DGCellInfo = New-Object System.Windows.Controls.DataGridCellInfo($DGCell)
$dg.SelectedCells.add($DGCellInfo)
$global:SearchIndex = $i
break
}
}
#check if we hit the end of the table. If we did display a notice and reset the search index.
If($global:SearchIndex -ge $dg.Items.Count){
[System.Windows.Forms.MessageBox]::Show("No more matches found, resetting search to begining of table.")
$global:SearchIndex=0
}
})
$global:SearchIndex = 0
$global:SearchIndex2 = 0
$bt_Search.Add_Click({
$SearchValue = $tb_Search.text
$dg.SelectedCells.Clear()
for ($i = $global:SearchIndex; $i -lt $dg.Items.Count; $i++)
{
$global:BreakPoint = $false
For($j=$global:SearchIndex2;$j -le 2;$j++){
If($dg.Items[$i].Row[$j] -eq "$SearchValue"){
$dg.ScrollIntoView($dg.Items[$i],$dg.Columns[$j])
$DGCell = $dg.Columns[$j].GetCellContent($dg.Items[$i]).Parent
$DGCellInfo = New-Object System.Windows.Controls.DataGridCellInfo($DGCell)
$dg.SelectedCells.add($DGCellInfo)
$global:SearchIndex = $i
$global:SearchIndex2 = $j+1
$global:BreakPoint = $true
break
}
}
If($global:BreakPoint){break}
$global:SearchIndex2=0
}
#check if we hit the end of the table. If we did display a notice and reset the search index.
If($global:SearchIndex -ge $dg.Items.Count){
[System.Windows.Forms.MessageBox]::Show("No more matches found, resetting search to begining of table.")
$global:SearchIndex=0
$global:SearchIndex2=0
}
})