<Window x:Class="PerformanceMonitorDashboard.QueryStatsHistoryWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:converters="clr-namespace:PerformanceMonitorDashboard.Converters"
        xmlns:ScottPlot="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF"
        Title="Query Stats History" Height="900" Width="1600"
        WindowStartupLocation="CenterOwner"
        Background="{DynamicResource BackgroundBrush}">
    <Window.Resources>
        <converters:NullToBooleanConverter x:Key="NullToBooleanConverter"/>

        <!-- Context Menu for DataGrid Copy/Export -->
        <ContextMenu x:Key="DataGridContextMenu">
            <MenuItem Header="Copy Cell" Click="CopyCell_Click">
                <MenuItem.Icon><TextBlock Text="&#x1F4CB;"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy Row" Click="CopyRow_Click">
                <MenuItem.Icon><TextBlock Text="&#x1F4C4;"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click">
                <MenuItem.Icon><TextBlock Text="&#x1F4D1;"/></MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click">
                <MenuItem.Icon><TextBlock Text="&#x1F4CA;"/></MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="3*" MinHeight="300"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Header -->
        <Border Grid.Row="0" Background="{DynamicResource BackgroundLightBrush}" Padding="10" Margin="10,10,10,0">
            <StackPanel>
                <TextBlock x:Name="QueryIdentifierText" FontSize="16" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}"/>
                <TextBlock x:Name="SummaryText" Margin="0,5,0,0" Foreground="{DynamicResource ForegroundBrush}"/>
            </StackPanel>
        </Border>

        <!-- Chart Area -->
        <Border Grid.Row="1" Background="{DynamicResource BackgroundLightBrush}" Margin="10,10,10,0" Padding="10">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <!-- Chart Controls -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,10">
                    <TextBlock Text="Metric:" VerticalAlignment="Center" Foreground="{DynamicResource ForegroundBrush}" Margin="0,0,8,0"/>
                    <ComboBox x:Name="MetricSelector" Width="200" SelectionChanged="MetricSelector_SelectionChanged">
                        <ComboBoxItem Content="Avg Duration (ms)" Tag="AvgElapsedTimeMs" IsSelected="True"/>
                        <ComboBoxItem Content="Avg CPU (ms)" Tag="AvgWorkerTimeMs"/>
                        <ComboBoxItem Content="Avg Logical Reads" Tag="AvgLogicalReads"/>
                        <ComboBoxItem Content="Avg Logical Writes" Tag="AvgLogicalWrites"/>
                        <ComboBoxItem Content="Avg Physical Reads" Tag="AvgPhysicalReads"/>
                        <ComboBoxItem Content="Avg Rows" Tag="AvgRows"/>
                        <ComboBoxItem Content="Executions Per Interval" Tag="IntervalExecutions"/>
                    </ComboBox>
                </StackPanel>

                <!-- Chart -->
                <ScottPlot:WpfPlot x:Name="HistoryChart" Grid.Row="1"/>
            </Grid>
        </Border>

        <!-- Data Grid -->
        <DataGrid Grid.Row="2" x:Name="HistoryDataGrid"
                  AutoGenerateColumns="False"
                  IsReadOnly="True"
                  CanUserSortColumns="True"
                  AlternatingRowBackground="{DynamicResource BackgroundLightBrush}"
                  Background="{DynamicResource BackgroundBrush}"
                  Foreground="{DynamicResource ForegroundBrush}"
                  RowBackground="{DynamicResource BackgroundBrush}"
                  GridLinesVisibility="All"
                  RowHeight="35"
                  ContextMenu="{DynamicResource DataGridContextMenu}"
                  Margin="10">
            <DataGrid.Columns>
                <!-- Collection Info -->
                <DataGridTextColumn Binding="{Binding CollectionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectionTime" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Collection Time" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding LastExecutionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastExecutionTime" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Last Execution" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding CreationTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CreationTime" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Creation Time" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding ObjectType}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ObjectType" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Object Type" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Execution Count (per-interval, computed from cumulative counter) -->
                <DataGridTextColumn Binding="{Binding IntervalExecutions, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IntervalExecutions" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Exec Count" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding ExecutionCountDelta, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="85">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ExecutionCountDelta" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Exec Delta" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Worker Time (CPU) - Total/Avg/Min/Max -->
                <DataGridTextColumn Binding="{Binding TotalWorkerTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="110">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalWorkerTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Total CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding AvgWorkerTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgWorkerTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinWorkerTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinWorkerTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxWorkerTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxWorkerTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TotalWorkerTimeDeltaMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="105">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalWorkerTimeDeltaMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="CPU Delta (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Elapsed Time - Total/Avg/Min/Max -->
                <DataGridTextColumn Binding="{Binding TotalElapsedTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="125">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalElapsedTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Total Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding AvgElapsedTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgElapsedTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinElapsedTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinElapsedTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxElapsedTimeMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxElapsedTimeMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TotalElapsedTimeDeltaMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="125">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalElapsedTimeDeltaMs" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Duration Delta (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Logical Reads -->
                <DataGridTextColumn Binding="{Binding TotalLogicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="125">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalLogicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Total Logical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding AvgLogicalReads, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLogicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Logical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TotalLogicalReadsDelta, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="95">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalLogicalReadsDelta" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Reads Delta" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Physical Reads -->
                <DataGridTextColumn Binding="{Binding TotalPhysicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="130">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalPhysicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Total Physical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding AvgPhysicalReads, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgPhysicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Physical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinPhysicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinPhysicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Physical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxPhysicalReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="120">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxPhysicalReads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Physical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TotalPhysicalReadsDelta, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalPhysicalReadsDelta" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Phys Reads Delta" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Logical Writes -->
                <DataGridTextColumn Binding="{Binding TotalLogicalWrites, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="125">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalLogicalWrites" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Total Logical Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding AvgLogicalWrites, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="115">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLogicalWrites" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Logical Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding TotalLogicalWritesDelta, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="95">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalLogicalWritesDelta" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Writes Delta" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Rows -->
                <DataGridTextColumn Binding="{Binding TotalRows, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalRows" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Total Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding AvgRows, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgRows" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Avg Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinRows, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="85">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinRows" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxRows, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="85">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxRows" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Parallelism -->
                <DataGridTextColumn Binding="{Binding MinDop}" ElementStyle="{StaticResource NumericCell}" Width="70">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinDop" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min DOP" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxDop}" ElementStyle="{StaticResource NumericCell}" Width="70">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxDop" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max DOP" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Memory Grants -->
                <DataGridTextColumn Binding="{Binding MaxGrantMb, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="105">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxGrantMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Grant (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxUsedGrantMb, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="105">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxUsedGrantMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Used (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxIdealGrantMb, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="105">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxIdealGrantMb" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Ideal (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Thread Usage -->
                <DataGridTextColumn Binding="{Binding MinUsedThreads}" ElementStyle="{StaticResource NumericCell}" Width="85">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinUsedThreads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Threads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxUsedThreads}" ElementStyle="{StaticResource NumericCell}" Width="85">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxUsedThreads" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Threads" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Spills -->
                <DataGridTextColumn Binding="{Binding TotalSpills, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalSpills" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Total Spills" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MinSpills, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="85">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinSpills" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Min Spills" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MaxSpills, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="85">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxSpills" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Max Spills" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- CLR Time -->
                <DataGridTextColumn Binding="{Binding TotalClrTime, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="100">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalClrTime" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Total CLR Time" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Sample Interval -->
                <DataGridTextColumn Binding="{Binding SampleIntervalSeconds}" ElementStyle="{StaticResource NumericCell}" Width="85">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SampleIntervalSeconds" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Interval (sec)" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Query Identifiers -->
                <DataGridTextColumn Binding="{Binding SqlHandle}" Width="160">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SqlHandle" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="SQL Handle" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding PlanHandle}" Width="160">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PlanHandle" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Plan Handle" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding QueryHash}" Width="160">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryHash" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Query Hash" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding QueryPlanHash}" Width="160">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Horizontal">
                            <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryPlanHash" Click="Filter_Click" Margin="0,0,4,0"/>
                            <TextBlock Text="Query Plan Hash" FontWeight="Bold" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>

                <!-- Download Plan Button -->
                <DataGridTemplateColumn Header="Query Plan" Width="Auto" MinWidth="90">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Download" Click="DownloadPlan_Click"
                                    HorizontalAlignment="Center" VerticalAlignment="Center"
                                    Padding="8,2"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <!-- Footer -->
        <Border Grid.Row="3" Background="{DynamicResource BackgroundLightBrush}" Padding="10" Margin="10,0,10,10">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Content="Close" Width="100" Height="30" Click="Close_Click"/>
            </StackPanel>
        </Border>
    </Grid>
</Window>
