我有一个ListBox
填充TextBox
es 的项目。当从ListBox
. 这是我的 XAML 代码ListBox
:
<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();