gpt4 book ai didi

c# - 列表框.SelectedItem

转载 作者:行者123 更新时间:2023-11-30 15:38:43 27 4
gpt4 key购买 nike

我有一个ListBox,其中包含填充TextBoxes 的项目。当从 ListBox 中进行选择时,如何识别选择的文本字符串。这是我的 ListBox XAML 代码:

<StackPanel x:Name="InputPanel" Orientation="Horizontal" HorizontalAlignment="Left">
<StackPanel>
<TextBlock Text="Input" Style="{StaticResource H2Style}"/>
<TextBlock Text="Select Scenario:" Style="{StaticResource H3Style}"/>
<ListBox x:Name="ScenarioList" Margin="0,0,20,0" HorizontalAlignment="Left" SelectionChanged="ScenarioList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBoxItem x:Name="Scenario1">
<TextBlock Style="{StaticResource ListBoxTextStyle}" Text="1) Pick a single photo" />
</ListBoxItem>
<ListBoxItem x:Name="Scenario2">
<TextBlock Style="{StaticResource ListBoxTextStyle}" Text="2) Pick multiple files" />
</ListBoxItem>
<ListBoxItem x:Name="Scenario3">
<TextBlock Style="{StaticResource ListBoxTextStyle}" Text="3) Pick a folder" />
</ListBoxItem>
<ListBoxItem x:Name="Scenario4">
<TextBlock Style="{StaticResource ListBoxTextStyle}" Text="4) Save a file" />
</ListBoxItem>
</ListBox>
</StackPanel>

我已经在我的 selection_changed 方法中尝试了各种方法。这是最新的:

object selectedItem = ScenarioList.SelectedItem;
ListBoxItem selected = this.ScenarioList.ItemContainerGenerator.ContainerFromItem(this.ScenarioList.SelectedItem) as ListBoxItem;
string tempStr = selected.Content.ToString();

最佳答案

ListBoxItem listBox_Item = listBox.SelectedItem as ListBoxItem;
MessageBox.Show("You have selected " + listBox_Item.Content.ToString());

或者你可以在 Selection changed 事件上试试这个

 private void ScenarioList_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{
if (((ListBox)sender).SelectedItem != null)
MessageBox.Show("You have selected " + (ListBox)sender).SelectedItem);
}

关于c# - 列表框.SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11110778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com