从 ListView 或 ListBox 项中获取 value/text
Get value/text from ListView or ListBox item
我在从 ListView 所选项目中获取值(文本)时遇到问题。
此外,我没有使用MVVM,它是powershell runspace。
这里是 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,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>
这是我遇到问题的 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,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>
现在 SelectedValue
包含 ListViewItem.Content
的值,因此请在 PowerShell 中读取它。
$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteList.SelectedValue.ToString()})
我在从 ListView 所选项目中获取值(文本)时遇到问题。
此外,我没有使用MVVM,它是powershell runspace。
这里是 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,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>
这是我遇到问题的 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,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>
现在 SelectedValue
包含 ListViewItem.Content
的值,因此请在 PowerShell 中读取它。
$SiteCheck = $SyncHash.Window.Dispatcher.invoke([System.Func[String]{$SyncHash.SiteList.SelectedValue.ToString()})