title: Get value/text from ListView or ListBox item
body: <p>I have an issue with getting the value (text) from ListView selected item.</p>
  <p>Besides, I am not using MVVM, it is powershell runspace.</p>
  <p>Here is the XAML code:</p>
  <pre><code>&lt;Window x:Class=&quot;Post_Depl_App.MainWindow&quot;
          xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
          xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
          xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
          xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
          xmlns:local=&quot;clr-namespace:Post_Depl_App&quot;
          mc:Ignorable=&quot;d&quot;
          Title=&quot;Post_Depl_App&quot; WindowState=&quot;Maximized&quot; ResizeMode=&quot;NoResize&quot; WindowStartupLocation=&quot;CenterScreen&quot; Topmost=&quot;false&quot; Background=&quot;#0060a9&quot;&gt;
          &lt;Window.Resources&gt;
          &lt;BooleanToVisibilityConverter x:Key=&quot;b2v&quot; /&gt;
          
          &lt;Style x:Key=&quot;FocusTextBox&quot; TargetType=&quot;Grid&quot;&gt;
          &lt;Style.Triggers&gt;
              &lt;DataTrigger Binding=&quot;{Binding ElementName=NumText, Path=IsVisible}&quot; Value=&quot;True&quot;&gt;
                  &lt;Setter Property=&quot;FocusManager.FocusedElement&quot; Value=&quot;{Binding ElementName=NumText}&quot;/&gt;
             &lt;/DataTrigger&gt;
          &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
          
      &lt;/Window.Resources&gt;
  
  &lt;Grid&gt;
      
   &lt;ListView Name=&quot;SiteList&quot; Height=&quot;530&quot; Width=&quot;700&quot; HorizontalAlignment=&quot;Center&quot; Margin=&quot;0,350,0,100&quot; Background=&quot;Transparent&quot; BorderThickness=&quot;0&quot; VerticalAlignment=&quot;Top&quot; 
          Foreground=&quot;White&quot; FontFamily=&quot;Segoe UI SemiLight&quot; FontSize=&quot;20&quot; 
          VirtualizingStackPanel.IsVirtualizing=&quot;True&quot; ItemsSource=&quot;{Binding}&quot; ScrollViewer.VerticalScrollBarVisibility=&quot;Hidden&quot; ScrollViewer.CanContentScroll=&quot;False&quot; ScrollViewer.IsDeferredScrollingEnabled=&quot;True&quot;
          SelectedIndex=&quot;0&quot;&gt;    
                  &lt;ListViewItem Content = &quot;Australia&quot;&gt;&lt;/ListViewItem&gt;
                  &lt;ListViewItem Content = &quot;Chile&quot;&gt;&lt;/ListViewItem&gt;
          &lt;/ListView&gt;
  </code></pre>
  <p>And here is the powershell where I have troubles. Probably finding the right property:</p>
  <pre><code>$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteList.SelectedItem.ToString()})
  
          if($SiteCheck -eq &quot;Australia&quot;){
              (Get-Content -path C:\Post_Setup\temp.txt -Raw) -replace 'SiteVar','AU' | Set-Content -Path C:\Post_Setup\temp.txt
          }
          elseif($SiteCheck -eq &quot;Chile&quot;){
              (Get-Content -path C:\Post_Setup\temp.txt -Raw) -replace 'SiteVar','CL' | Set-Content -Path C:\Post_Setup\temp.txt
          }
  </code></pre>
  <p>I suppose the issue lies in this line:</p>
  <pre><code>$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteList.SelectedItem.ToString()})
  </code></pre>
  <p>It doesn't work for ListView or ListBox.
  However this works perfectly for ComboBox:</p>
  <pre><code>$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteBox.Text})
  </code></pre>
  <p>Does anybody know the best way to get selected item value as a text from ListView/Box?</p>
  <p>Thank you.</p>