如何防止WPF Datagrid在标题单击时恢复为defaultview

时间:2016-10-05 21:16:59

标签: wpf powershell datagrid

我真的希望有人能指出我正确的方向。我是PowerShell / WPF的新手,但我正在尽可能多地阅读/观看。

我在底部的链接中有一些代码示例,文本框充当过滤器,输入svchost并单击过滤器按钮只是执行get-process,通过过滤器过滤它并将其输出到datagrid 。问题是,如果我单击datagrid的标题,则替换过滤后的视图并列出所有进程。

我一直在关注这个问题,并认为它与数据网格相关,而不是当前视图而不是当前视图,但不确定正确的解决方法。

$xaml = @'
<Window
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Width ="400"
   SizeToContent="Height"
   Title="Example"
   Topmost="True">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="400"/>
        </Grid.RowDefinitions>
        <TextBox x:Name="txtbx_Filter" Grid.Column="1" Height="20 "  VerticalContentAlignment="Center" VerticalAlignment="Bottom" />
        <DataGrid 
             x:Name="dataGrid" 
             Grid.Column="0"
             Grid.Row="1"
             Grid.ColumnSpan="2"
             IsReadOnly="True" 
             SelectionMode="Single" 
             BorderThickness="0" 
             AlternatingRowBackground="#FFEFFBFB"
             AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Process Name"
                                     Binding="{Binding Name}"
                                     Width="2*"
                                     CanUserResize="False"
                                     />
            </DataGrid.Columns>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="FontWeight"  Value="Bold" />
                    <Setter Property="HorizontalAlignment" Value="Center" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                    <Setter Property="HorizontalContentAlignment" Value="Center" />
                </Style>
            </DataGrid.ColumnHeaderStyle>
        </DataGrid>
        <Button x:Name="Btn_Filter" Content="Filter" Height="20"  VerticalAlignment="Bottom"/>

    </Grid>
</Window>
'@
#endregion

#region Code Behind
function Convert-XAMLtoWindow
{
  param
  (
    [Parameter(Mandatory)]
    [string]
    $XAML,

    [string[]]
    $NamedElement=$null,

    [switch]
    $PassThru
  )

  Add-Type -AssemblyName PresentationFramework

  $reader = [XML.XMLReader]::Create([IO.StringReader]$XAML)
  $result = [Windows.Markup.XAMLReader]::Load($reader)
  foreach($Name in $NamedElement)
  {
    $result | Add-Member NoteProperty -Name $Name -Value $result.FindName($Name) -Force
  }

  if ($PassThru)
  {
    $result
  }
  else
  {
    $null = $window.Dispatcher.InvokeAsync{
      $result = $window.ShowDialog()
      Set-Variable -Name result -Value $result -Scope 1
    }.Wait()
    $result
  }
}

function Show-WPFWindow
{
  param
  (
    [Parameter(Mandatory)]
    [Windows.Window]
    $Window
  )

  $result = $null
  $null = $window.Dispatcher.InvokeAsync{
    $result = $window.ShowDialog()
    Set-Variable -Name result -Value $result -Scope 1
  }.Wait()
  $result
}
#endregion Code Behind

#region Convert XAML to Window
$window = Convert-XAMLtoWindow -XAML $xaml -NamedElement 'Btn_Filter', 'dataGrid', 'txtbx_Filter' -PassThru

$dataGrid = $window.FindName('dataGrid')

$window.Btn_Filter.add_Click{
    $myfilter = $window.txtbx_Filter.Text
    $process = Get-Process
    $a = New-Object -TypeName System.Collections.ObjectModel.ObservableCollection[object]
    $process | ForEach-Object -Process {
        $a.Add((
                New-Object -TypeName PSObject -Property @{
                    Name         = $_.Name
                }
        ))      
    }
    $view = [System.Windows.Data.CollectionViewSource]::GetDefaultView($a)
    $filter = "$myfilter"
    $view.Filter = {
        param ($item) $item -match $filter
    }
    $view.Refresh()
    $dataGrid.ItemsSource = $view  
}


# Show Window
$result = Show-WPFWindow -Window $window

1 个答案:

答案 0 :(得分:0)

我无法相信,事实证明它是如此简单。

当我按下标题按钮时,我可以看到循环通过第128,129,130​​行的代码,$ filter的变量突然变空,这正在重置过滤器。

要解决我必须要解决的问题:

    $view.Filter = {
    param ($item) $item -match $window.txtbx_Filter.Text
}

我喜欢ISESteroids! enter image description here