我正在开发windows phone 8 app。我的应用程序的某些部分需要用户响应 我创建Windows Phone用户控制页面。用户将从usercontrol页面中的列表框中选择一个值
这是我的用户控制页面的代码
<Grid x:Name="columngrid" Background="#FF1FCB4E" Width="480" >
<Grid.RowDefinitions>
<RowDefinition Height="300"/>
<RowDefinition Height="70"/>
<RowDefinition Height="70"/>
</Grid.RowDefinitions>
<ListBox Name="URLListBox" Grid.Row="0" Background="#FF1FCB4E" >
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="#FF0B232F" BorderThickness="2">
<TextBlock x:Name="surename" Width="460" Tag="{Binding b1Tag}" Height="80" FontSize="25" Text="{Binding text}" Foreground="#FFBF9595" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="1" x:Name="btnOK" Content="OK" Background="#FF892121" />
<Button Grid.Row="2" x:Name="btnCancel" Content="Cancel" Background="#FF892121"/>
</Grid>
我想在mainpage.xaml中获取所选文本块的文本。但是当我为用户控制页面创建类对象时,我无法访问文本块。我只能到达列表框。如何获取所选文本块的文本 我试过这个,但我意识到s会带来列表框对象
surah popupsurah = new surah();//usercontrol page
Popup popup2 = new Popup();
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
collapsedgrid.Visibility = Visibility.Collapsed;
popupsurah.URLListBox.Tap += (s, args) =>
{
string transferID = ((TextBlock)s).Text as string;
答案 0 :(得分:0)
最好的方法是使用ListBox SelectedItem属性进行绑定,这样您就可以轻松地从对象本身中检索它。
如下所示,(算法)
你可以上课:
class MyClass
{
List<string> Items{get;set;}
string SelectedItem{get;set;}
}
您可以将此类的实例传递给UserControl:
MyUserControl(MyClass);
在MyUserControl的构造函数中:
设置DataContext:this.DataContext = MyClass;
将属性名称绑定在xaml中。多数民众赞成。
另一种方法是:
将选定的Item字符串保存到状态字典中:
PhoneApplicationService.Current.State["SelectedItem"]=YourSelectedItemString;
您可以在MainPage.xaml中检索如下:
var selectedItem=PhoneApplicationService.Current.State["SelectedItem"] as string;
答案 1 :(得分:0)
最好你可以使用Phone:LongListSelector控件,它比ListBox更优化,ListBox几乎包含ListBox的所有事件和属性。