0
votes

So maybe I am approaching this the wrong way, feel free to give any advice or tips. Currently working with a DataGrid table in wpf using powershell to do all the logic. Here is the table:

enter image description here

What is the best way to properly extract all the data INCLUDING the combobox selected value from a selected row? Currently I am trying this:

$test = $AddServerwpf.ServerGrid.SelectedItems[0] 
Write-Host "Selected: $test"

For context $AddServerwpf contains all the objects for that window. ServerGrid is the DataGrid object. I am using .SelectedItems[0] to get that row.

This returns:

Selected: @{Server=server1; Environment=Prod; ServiceAccount=System.Object[]}

If I go one step further with:

$AddServerwpf.ServerGrid.SelectedItems[0].ServiceAccount

I get:

Selected: account1 account2 account3

This obviously doesn't tell me which account was selected from the drop down. How can I get the combobox selection? I have looked pretty deeply through google with other questions about this and I have not found a working answer. How do I properly bind the combobox to the datagrid? Or is it better to get to the Combobox object somehow and extract the text?

Here is the XAML:

<Window x:Class="ServerManagmentApp.AddServer" x:Name="AddServerWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ServerManagmentApp"
    mc:Ignorable="d"
    Title="AddServer" Height="359.7" Width="387.4" Background="#FF2B2929">
<Grid>
    <Button x:Name="AddServerButton" Content="Add Server" HorizontalAlignment="Left" Margin="220,260,0,0" VerticalAlignment="Top" Width="120" Height="40" Background="#FF1FD14F"/>
    <DataGrid x:Name="ServerGrid" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="213" Width="275" Background="#FF888F8A" SelectionMode="Single">

        <DataGrid.Columns>

            <DataGridTextColumn Header="Server" Binding="{Binding Server}" Width="*" />
            <DataGridTextColumn Header="Environment" Binding="{Binding Environment}" Width="*" />

            <DataGridTemplateColumn Header="ServiceAccount" Width="*">
                <DataGridTemplateColumn.CellTemplate>

                    <DataTemplate>
                        <ComboBox x:Name="ServiceAccount" ItemsSource="{Binding ServiceAccount}" SelectedIndex="0"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
</Window>

Spent a couple hours trying to get past this roadblock and any help would be appreciated. I am new to this and trying to wrap my head around how this binding works while using powershell.

EDIT: Here is how I am populating the DataGrid if that helps

$list = "account1","account2","account3"
$AddServerwpf.ServerGrid.AddChild([pscustomobject{Server='server1';Environment='Prod';ServiceAccount=$list})
$AddServerwpf.ServerGrid.AddChild([pscustomobject]@{Server='server2';Environment='Prod';ServiceAccount=$list})
$AddServerwpf.ServerGrid.AddChild([pscustomobject]@{Server='server3';Environment='Prod';ServiceAccount=$list})
1

1 Answers

1
votes

You should bind the SelectedItem property of the ComboBox to a property of your data object where the Server, Environment and ServiceAccount properties are already defined:

<ComboBox x:Name="ServiceAccount" ItemsSource="{Binding ServiceAccount}" 
    SelectedItem={Binding SelectedAccount, UpdateSourceTrigger=PropertyChanged}" />

$list = "account1","account2","account3"
$AddServerwpf.ServerGrid.AddChild([pscustomobject{Server='server1';Environment='Prod';ServiceAccount=$list;SelectedAccount='account1'})
$AddServerwpf.ServerGrid.AddChild([pscustomobject]@{Server='server2';Environment='Prod';ServiceAccount=$list;SelectedAccount='account1'})
$AddServerwpf.ServerGrid.AddChild([pscustomobject]@{Server='server3';Environment='Prod';ServiceAccount=$list;SelectedAccount='account1'})

You can then get the selected value of an item using this property, e.g.:

$AddServerwpf.ServerGrid.SelectedItems[0].SelectedAccount