访问ListBox中的PhoneTextBox

时间:2013-05-06 10:03:40

标签: c# xaml windows-phone-7.1

我想从名为" buttonSearch"的PhoneTextBox访问值放在ListBox中。唯一的问题是我无法从后面的代码中调用它。我的意思是我可以轻松地这样做:

string writtenText = buttonSearch.Text;

当PhoneTextBox不在ListBox内时。

MainPage.xaml中:

<controls:PanoramaItem Name="panoramaSearch">
            <ListBox ItemsSource="{Binding ApiSearch}" Name="listboxSearch">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" x:Name="panelSearch">
                            <toolkit:PhoneTextBox x:Name="buttonSearch" Hint="search" ActionIcon="Images/Search.png" Height="70" Width="350" ActionIconTapped="SearchIconTapped" />
                            <StackPanel Orientation="Horizontal">
                                <Image Height="100" Width="100" Source="{Binding Image}" />
                                <StackPanel Width="311">
                                    <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Style="{StaticResource PhoneTextTitle3Style}" />
                                    <TextBlock Text="{Binding Category}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSmallStyle}" />
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PanoramaItem>

MainPage.xaml.cs中:

 private void SearchIconTapped(object sender, EventArgs e) {
     //here is place i want to get PhoneTextBoxValue
 }

我的问题是我可以访问的是名为listboxSearch的ListBox。我不能直接从PhoneTextBox获得价值。有没有办法获得这个价值?

提前致谢。

1 个答案:

答案 0 :(得分:0)

在MainPage.xaml.cs中创建依赖项属性,我们称之为PhoneText

在您的数据模板中添加到PhoneTextBox:

Text="{Binding PhoneText, RelativeSource={RelativeSource FindAncestor, AncestorType=Page}, Mode=TwoWay}"

然后回到代码隐藏:

private void SearchIconTapped(object sender, EventArgs e) {
    var phoneTxt = PhoneText; //not really necessary, just to show usage
}