问题描述
我在从 ListView 所选项目获取值(文本)时遇到问题。
此外,我没有使用 MVVM,它是 powershell 运行空间。
这是 XAML 代码:
<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,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>
这是我遇到麻烦的 powershell。可能找到合适的属性:
$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
}
我想问题出在这一行:
$SiteCheck = $SyncHash.Window.dispatcher.invoke([System.Func[String]{$SyncHash.SiteList.SelectedItem.ToString()})
它不适用于 ListView 或 ListBox。 然而,这对于 ComboBox 非常有效:
$SiteCheck = $SyncHash.Window.dispatcher.invoke([System.Func[String]{$SyncHash.SiteBox.Text})
有人知道从 ListView/Box 中以文本形式获取所选项目值的最佳方法吗?
谢谢。
解决方法
在您的情况下,SelectedItem
的类型是 ListViewItem
。您需要获取 ListViewItem.Content
属性的值。
您可以指定 SelectedValuePath
是用于获取 SelectedItem
的 SelectedValue
中的路径(属性名称)。
<ListView Name="SiteList" Height="530" Width="700" HorizontalAlignment="Center" Margin="0,350,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>
现在 SelectedValue
包含 ListViewItem.Content
的值,因此请在 PowerShell 中读取它。
$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteList.SelectedValue.ToString()})