I have an issue with getting the value (text) from ListView selected item.
Besides, I am not using MVVM, it is powershell runspace.
Here is the XAML code:
<Window x:Class="Post_Depl_App.MainWindow"
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:Post_Depl_App"
mc:Ignorable="d"
Title="Post_Depl_App" WindowState="Maximized" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Topmost="false" Background="#0060a9">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="b2v" />
<Style x:Key="FocusTextBox" TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=NumText, Path=IsVisible}" Value="True">
<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=NumText}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<ListView Name="SiteList" Height="530" Width="700" HorizontalAlignment="Center" Margin="0,350,0,100" Background="Transparent" BorderThickness="0" VerticalAlignment="Top"
Foreground="White" FontFamily="Segoe UI SemiLight" FontSize="20"
VirtualizingStackPanel.IsVirtualizing="True" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.CanContentScroll="False" ScrollViewer.IsDeferredScrollingEnabled="True"
SelectedIndex="0">
<ListViewItem Content = "Australia"></ListViewItem>
<ListViewItem Content = "Chile"></ListViewItem>
</ListView>
And here is the powershell where I have troubles. Probably finding the right property:
$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteList.SelectedItem.ToString()})
if($SiteCheck -eq "Australia"){
(Get-Content -path C:\Post_Setup\temp.txt -Raw) -replace 'SiteVar','AU' | Set-Content -Path C:\Post_Setup\temp.txt
}
elseif($SiteCheck -eq "Chile"){
(Get-Content -path C:\Post_Setup\temp.txt -Raw) -replace 'SiteVar','CL' | Set-Content -Path C:\Post_Setup\temp.txt
}
I suppose the issue lies in this line:
$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteList.SelectedItem.ToString()})
It doesn't work for ListView or ListBox. However this works perfectly for ComboBox:
$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteBox.Text})
Does anybody know the best way to get selected item value as a text from ListView/Box?
Thank you.
In your case the type of SelectedItem
is ListViewItem
. You need to get a value of ListViewItem.Content
property.
You can specify SelectedValuePath
which is the path (property name) in SelectedItem
used to get the SelectedValue
.
<ListView Name="SiteList" Height="530" Width="700" HorizontalAlignment="Center" Margin="0,350,0,100" Background="Transparent" BorderThickness="0" VerticalAlignment="Top"
Foreground="White" FontFamily="Segoe UI SemiLight" FontSize="20"
VirtualizingStackPanel.IsVirtualizing="True" ItemsSource="{Binding}" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.CanContentScroll="False" ScrollViewer.IsDeferredScrollingEnabled="True"
SelectedIndex="0" SelectedValuePath="Content">
<ListViewItem Content = "Australia"></ListViewItem>
<ListViewItem Content = "Chile"></ListViewItem>
</ListView>
Now the SelectedValue
contains value of ListViewItem.Content
, so read it in the PowerShell.
$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteList.SelectedValue.ToString()})