2014-07-11 30 views
0

我正在开发Windows Phone 8应用程序。我需要在我的应用程序的某些部分的用户响应 我创建Windows Phone用户控制页面。用户将从列表框中选择一个值在用户控件页如何获取列表框中选择的项目标签在主页xaml

这里是我的用户控制网页

<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.But当我在用户控制网页我不能达到文本块创建类的对象。我只能到达listbox.How我可以得到选定的文本块的文本 我试试这个,但我意识到旨意带来ListBox对象

 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

这里是一个示例帮助你出http://www.geekchamp.com/tips/how-to-expose-properties-of-a-user-control-in-windows-phone –

回答

0

最好的办法是做与列表框SelectedItem属性绑定,所以你可以很容易地从对象本身获取它。

类似下面,(算法)

你可以有一个类:

class MyClass 
{ 
List<string> Items{get;set;} 
string SelectedItem{get;set;} 
} 

您可以通过这个类的一个实例到该用户控件:

MyUserControl(MyClass); 

在构造函数MyUserControl:

设置DataContext:this.DataContext = MyClass;

绑定xaml中的属性名称。而已。

另一种方法是:

保存所选项目串入状态字典:

PhoneApplicationService.Current.State["SelectedItem"]=YourSelectedItemString; 

您可以在MainPage.xaml中获取这样的:

var selectedItem=PhoneApplicationService.Current.State["SelectedItem"] as string; 
+0

我有一个问题,我不能从usercontrolpagexaml.cs中获取列表框中的文本 – user3820387

+0

您可以使用ListBoxName.SelectedItem或ListBoxName.SelectedIndex从OnSelectionChanged事件中获取该文本;如果你是ListBox的Binding ItemsSource属性,那么你必须TypeCast你的SelectedItem为:ListBoxName.SelectedItem作为ViewModelType; –

0

你可以更好去Phone:LongListSelector控件,它比ListBox更优化,它几乎包含了ListBox的所有事件和属性。

相关问题