我正在创建一个WP 8.1 Silverlight应用程序。
我有一个字符串名称的ObservableCollection
,它被设置为ListBox
的ItemsSource
。这些名称是ListBox
中按钮的名称。然后,我想从ListBox
中提取按钮,但返回值的类型是string
。
xaml代码如下:
<ListBox x:Name="Game_ScrollViewer_online" Margin="41,104,128,6" SelectedValuePath="Current_game_button">
<ListBox.ItemTemplate>
<DataTemplate>
<Button x:Name="Current_game_button" Content="{Binding}"
HorizontalAlignment="Center" Height="80" Margin="-14,6,-15,0"
VerticalAlignment="Top" Width="210" Template="{StaticResource Button_CurrentLayout1}"
RenderTransformOrigin="0.5,0.5" Foreground="#FFCBECCB" FontFamily="Times New Roman"
Click="LoadGame_online" FontSize="16">
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我希望您能提取按钮元素。
for (int i = 0; i < Game_ScrollViewer_online.Items.Count; i++)
{
var tempType = Game_ScrollViewer_online.Items[i].GetType();
Button tempBut = (Game_ScrollViewer_online.Items[i] as Button);
//Do stuff with button
}
但是返回类型是字符串。
为什么不是按钮?是否有访问按钮的方法?
FindDescendant
就可以了。更新你的答案,我会标记为已解决 :) - JTIM