2011-04-29 82 views
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 

編輯: 我改變了代碼,XAML如何通過單擊此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) { 

[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 

但是,當我加入由托馬斯·萊維斯克提出的線

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'." 

我我猜我必須註冊一些程序集。

回答

2

用於XAML溶液

還可以使用使用GridViewSort.SetAutoSortGridViewSort.SetPropertyName方法在代碼此溶液參見this blog post(和this one)。我不知道PowerShell的語法,但在這裏它是在C#:

GridViewSort.SetAutoSort(TableView, true); 
GridViewSort.SetPropertyName(TitleColumn, "title"); 
+0

問題是究竟轉化XAML解決方案編寫 – 2011-04-29 10:29:33

+0

@bernd_k,看到我更新的答案 – 2011-04-29 12:30:48

+0

+1我認爲這表明正確的方向 – 2011-04-29 13:26:42

相關問題