How to get column and row index from Powershell XAML gui datagrid?

40 Views Asked by At

I usualy make gui's in winform but as this would be used by more people and its going to be quite large and I want to simplify it I wanted to use visual studio for this so wpf/xaml it is.

The issue I run is seems quite simple but somehow I am not finding a decent solution.

[xml]$xaml = @'
<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Window" Title="Demo" WindowStartupLocation = "CenterScreen" 
        SizeToContent="WidthAndHeight"
        ShowInTaskbar = "True">

    <StackPanel Orientation="Vertical" Name="msiStackPanel">
        <TextBlock Margin="20,20,20,20" HorizontalAlignment="Center" FontSize="14" FontWeight="Bold">List of Stuff</TextBlock>
        <DataGrid Name="myGrid">
            <DataGrid.Columns>
            </DataGrid.Columns>
        </DataGrid>
        <Button Name="UpdateDatabaseButton" Content="Update Database" Margin="10" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
    </StackPanel>
</Window>
'@

# Set PowerShell variable for each named object in GUI
Function Set-WpfVariables
{
  param($children)
    ForEach ($child in $children.Children)
    {
        Set-WpfVariables -Children $child
        if (![String]::IsNullOrEmpty($child.Name))
        {
            Write-Host "Set variable $($child.Name)"
            Set-Variable -Name $child.Name -Value $child -Scope global
        }    
    }
}

# Build Window
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$controls = [System.Windows.LogicalTreeHelper]::GetChildren($window)
Set-WpfVariables -Children $controls
$myGrid = $window.FindName("myGrid")
$Button = $window.FindName("UpdateDatabaseButton")

$myGrid.ItemsSource = get-process | Select-Object -First 5

$button.Add_Click({
      $rowIndex = $myGrid.SelectedIndex
     $columnIndex = $myGrid.CurrentColumn.DisplayIndex
     write-host "cell values selected : row   $rowIndex Column :  $columnIndex "
})

$myGrid.DataContext = $table.DefaultView
$window.ShowDialog()

A simple script that has some data in a datagrid the goal is to update some of the data and thus be able to select it and run some code on that selection.

yet everything I try simply doesnt seem to work :

$myGrid.ADD_mouseclick({})

Method invocation failed because [System.Windows.Controls.DataGrid] does not contain a method named 'ADD_mouseclick'.

At line:1 char:1

+ $myGrid.ADD_mouseclick({})

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

CategoryInfo : InvalidOperation: (:) [], RuntimeException

FullyQualifiedErrorId : MethodNotFound

or

$myGrid.add_CellClick($dataGridView_CellClick)

Method invocation failed because [System.Windows.Controls.DataGrid] does not contain a method named 'add_CellClick'.

At line:7 char:1

+ $myGrid.add_CellClick($dataGridView_CellClick)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

CategoryInfo : InvalidOperation: (:) [], RuntimeException

FullyQualifiedErrorId : MethodNotFound

Same goes for everything else I tried(included the example given above row isnt an issue, column I simply cant get) to get to directly interact with the datagrid

ANyone any idea how to get the column and row clicked or selected ?

Tried various ways to directly interact with the datagrid by .ADD_mouseclick({}) or such but they dont seem to work for datagrid in wpf

0

There are 0 best solutions below