如何通过单击此 GridView 中的列标题来添加排序?
ipmo WPK
$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString
$conn.Open()
function Invoke-sql1
{
param( [string]$sql,
[System.Data.SQLClient.SQLConnection]$connection
)
$cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
$ds = New-Object system.Data.DataSet
$da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
$da.fill($ds) | Out-Null
return $ds.tables[0]
}
function Show-Bockmarks ($conn) {
New-ListView -Name ListView -View {
New-GridView -AllowsColumnReorder -Columns {
New-GridViewColumn "title"
}
} -show -On_Loaded {
$ff_sql = @"
SELECT 'abc' title
union
SELECT 'xyz' title
union
SELECT 'efg' title
"@
$TableView = $window | Get-ChildControl ListView
$TableView.ItemsSource = @(Invoke-sql1 -sql $ff_sql -connection $conn)
}
}
Show-Bockmarks $conn
编辑: 我将代码转换为 XAML,
ipmo WPK
$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString
$conn.Open()
function Invoke-sql1
{
param( [string]$sql,
[System.Data.SQLClient.SQLConnection]$connection
)
$cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
$ds = New-Object system.Data.DataSet
$da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
$da.fill($ds) | Out-Null
return $ds.tables[0]
}
function Show-Bockmarks ($conn) {
[xml] $xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" >
<ListView ItemsSource="{Binding Persons}"
IsSynchronizedWithCurrentItem="True"
Name="Listview">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="title"
DisplayMemberBinding="{Binding title}"
/>
<GridViewColumn Header="itemid"
DisplayMemberBinding="{Binding itemid}"
/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
$ff_sql = @"
SELECT 'abc' title, 3 itemid
union
SELECT 'xyz' title, 2 itemid
union
SELECT 'efg' title, 1 itemid
"@
$TableView = $Form.FindName("Listview")
$TableView.ItemsSource = @(Invoke-sql1 -sql $ff_sql -connection $conn)
$Form.ShowDialog() #| out-null
}
Show-Bockmarks $conn
但是当我添加 Thomas Levesque 提出的行时,
ipmo WPK
$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString
$conn.Open()
function Invoke-sql1
{
param( [string]$sql,
[System.Data.SQLClient.SQLConnection]$connection
)
$cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
$ds = New-Object system.Data.DataSet
$da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
$da.fill($ds) | Out-Null
return $ds.tables[0]
}
function Show-Bockmarks ($conn) {
[xml] $xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:util="clr-namespace:TheNameSpace;assembly=TheAssembly"
Title="MainWindow" >
<ListView ItemsSource="{Binding Persons}"
IsSynchronizedWithCurrentItem="True"
util:GridViewSort.AutoSort="True"
Name="Listview">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="title"
DisplayMemberBinding="{Binding title}"
util:GridViewSort.PropertyName="title"
/>
<GridViewColumn Header="itemid"
DisplayMemberBinding="{Binding itemid}"
util:GridViewSort.PropertyName="itemid"
/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
$ff_sql = @"
SELECT 'abc' title, 3 itemid
union
SELECT 'xyz' title, 2 itemid
union
SELECT 'efg' title, 1 itemid
"@
$TableView = $Form.FindName("Listview")
$TableView.ItemsSource = @(Invoke-sql1 -sql $ff_sql -connection $conn)
$Form.ShowDialog() #| out-null
}
Show-Bockmarks $conn
我收到错误
Exception calling "Load" with "1" argument(s): "The property 'GridViewSort.AutoSort' does not exist in XML namespace 'clr-namespace:TheNameSpace;assembly=TheAssembly'. Line '0'
Position '0'."
,我想我必须注册一些程序集。
ipmo WPK
$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString
$conn.Open()
function Invoke-sql1
{
param( [string]$sql,
[System.Data.SQLClient.SQLConnection]$connection
)
$cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
$ds = New-Object system.Data.DataSet
$da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
$da.fill($ds) | Out-Null
return $ds.tables[0]
}
function Show-Bockmarks ($conn) {
New-ListView -Name ListView -View {
New-GridView -AllowsColumnReorder -Columns {
New-GridViewColumn "title"
}
} -show -On_Loaded {
$ff_sql = @"
SELECT 'abc' title
union
SELECT 'xyz' title
union
SELECT 'efg' title
"@
$TableView = $window | Get-ChildControl ListView
$TableView.ItemsSource = @(Invoke-sql1 -sql $ff_sql -connection $conn)
}
}
Show-Bockmarks $conn
Edit:
I transformed the code to XAML
ipmo WPK
$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString
$conn.Open()
function Invoke-sql1
{
param( [string]$sql,
[System.Data.SQLClient.SQLConnection]$connection
)
$cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
$ds = New-Object system.Data.DataSet
$da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
$da.fill($ds) | Out-Null
return $ds.tables[0]
}
function Show-Bockmarks ($conn) {
[xml] $xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" >
<ListView ItemsSource="{Binding Persons}"
IsSynchronizedWithCurrentItem="True"
Name="Listview">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="title"
DisplayMemberBinding="{Binding title}"
/>
<GridViewColumn Header="itemid"
DisplayMemberBinding="{Binding itemid}"
/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
$ff_sql = @"
SELECT 'abc' title, 3 itemid
union
SELECT 'xyz' title, 2 itemid
union
SELECT 'efg' title, 1 itemid
"@
$TableView = $Form.FindName("Listview")
$TableView.ItemsSource = @(Invoke-sql1 -sql $ff_sql -connection $conn)
$Form.ShowDialog() #| out-null
}
Show-Bockmarks $conn
But when I added the lines proposed by Thomas Levesque
ipmo WPK
$ConnectionString = $ConnectionString = "Server=localhost;Integrated Security=True"
$conn = new-object System.Data.SQLClient.SQLConnection
$conn.ConnectionString = $ConnectionString
$conn.Open()
function Invoke-sql1
{
param( [string]$sql,
[System.Data.SQLClient.SQLConnection]$connection
)
$cmd = new-object System.Data.SQLClient.SQLCommand($sql,$connection)
$ds = New-Object system.Data.DataSet
$da = New-Object System.Data.SQLClient.SQLDataAdapter($cmd)
$da.fill($ds) | Out-Null
return $ds.tables[0]
}
function Show-Bockmarks ($conn) {
[xml] $xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:util="clr-namespace:TheNameSpace;assembly=TheAssembly"
Title="MainWindow" >
<ListView ItemsSource="{Binding Persons}"
IsSynchronizedWithCurrentItem="True"
util:GridViewSort.AutoSort="True"
Name="Listview">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="title"
DisplayMemberBinding="{Binding title}"
util:GridViewSort.PropertyName="title"
/>
<GridViewColumn Header="itemid"
DisplayMemberBinding="{Binding itemid}"
util:GridViewSort.PropertyName="itemid"
/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Form=[Windows.Markup.XamlReader]::Load( $reader )
$ff_sql = @"
SELECT 'abc' title, 3 itemid
union
SELECT 'xyz' title, 2 itemid
union
SELECT 'efg' title, 1 itemid
"@
$TableView = $Form.FindName("Listview")
$TableView.ItemsSource = @(Invoke-sql1 -sql $ff_sql -connection $conn)
$Form.ShowDialog() #| out-null
}
Show-Bockmarks $conn
I get the error
Exception calling "Load" with "1" argument(s): "The property 'GridViewSort.AutoSort' does not exist in XML namespace 'clr-namespace:TheNameSpace;assembly=TheAssembly'. Line '0'
Position '0'."
I guess I have to register some assembly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅这篇博文(以及此一个)用于 XAML 解决方案
您还可以使用
GridViewSort.SetAutoSort
和GridViewSort.SetPropertyName
方法在代码中使用此解决方案。我不知道 Powershell 语法,但这是用 C# 编写的:See this blog post (and this one) for a XAML solution
You can also use this solution in code using the
GridViewSort.SetAutoSort
andGridViewSort.SetPropertyName
method. I don't know the Powershell syntax, but here it is in C#: