0

我想从位于 ListBox 内的名为“buttonSearch”的 PhoneTextBox 访问值。唯一的问题是我不能从后面的代码中调用它。我的意思是我可以很容易地这样做:

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 中获取价值。有没有办法获得这个价值?

提前致谢。

4

1 回答 1

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
}
于 2013-05-06T17:14:27.757 回答