C# 使用ListBox中的可视树帮助器访问DataTemplate中的项

C# 使用ListBox中的可视树帮助器访问DataTemplate中的项,c#,windows,windows-phone-7,xaml,windows-phone-8,C#,Windows,Windows Phone 7,Xaml,Windows Phone 8,我有以下数据模板: <DataTemplate x:Key="ToDoListBoxItemTemplate"> <Grid x:Name="item2Expanded" HorizontalAlignment="Left" VerticalAlignment="Top" Width="480" Background="{Binding Converter={StaticResource RowColour}}" MinHeight="81">

我有以下数据模板:

    <DataTemplate x:Key="ToDoListBoxItemTemplate">
        <Grid x:Name="item2Expanded" HorizontalAlignment="Left" VerticalAlignment="Top" Width="480"  Background="{Binding Converter={StaticResource RowColour}}" MinHeight="81">
            <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Width="420" Margin="60,0,0,0">
                <TextBox x:Name="taskTitle" HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ItemName}" VerticalAlignment="Top" Width="420" Background="{x:Null}" BorderBrush="{x:Null}" CaretBrush="#FF0080FF" SelectionBackground="#FFCFCFCF" Foreground="#FF4E4E4E" BorderThickness="3,3,3,6" FontSize="29.333" Style="{StaticResource listTextBoxTemplate}" InputScope="Text" SelectionForeground="#FF4E4E4E" KeyUp="taskTitle_KeyUp" LostFocus="taskTitle_LostFocus" Tap="taskTitle_Tap" IsReadOnly="True" Margin="0,1,0,0" DoubleTap="taskTitle_DoubleTap"/>
                <TextBox x:Name="taskDetail" HorizontalAlignment="Left" TextWrapping="Wrap" Text="Tea is an essential English beverage, it has a nice calming effect, and is often served alongside biscuits." VerticalAlignment="Top" Width="420" Background="{x:Null}" BorderBrush="{x:Null}" CaretBrush="#FF0080FF" SelectionBackground="#FFCFCFCF" Foreground="#FF878787" BorderThickness="3,0,3,6" FontSize="21.333" Style="{StaticResource listTextBoxTemplate}" InputScope="Text" SelectionForeground="#FF878787" Margin="0,-20,0,0" KeyUp="taskDetail_KeyUp" LostFocus="taskDetail_LostFocus" Padding="2,5,2,2" IsHitTestVisible="False"/>
                <Grid Height="170" Margin="0,-20,0,0">
                    <Button x:Name="chooseDateButton" Content="27/06/2013" HorizontalAlignment="Left" Margin="6,13,0,0" VerticalAlignment="Top" BorderBrush="#FF959595" Foreground="#FF959595" Width="157" HorizontalContentAlignment="Left" FontSize="20" Style="{StaticResource selectorButtonTemplate}"/>
                    <Button x:Name="chooseTimeButton" Content="12:00" HorizontalAlignment="Left" Margin="146,13,0,0" VerticalAlignment="Top" BorderBrush="#FF959595" Foreground="#FF959595" Width="99" HorizontalContentAlignment="Left" FontSize="20" Style="{StaticResource selectorButtonTemplate}"/>
                    <Button x:Name="setOrClearButton" Content="REMIND ME" HorizontalAlignment="Left" Margin="228,13,0,0" VerticalAlignment="Top" BorderBrush="#FF959595" Foreground="White" Width="180" FontSize="20" Background="#FF959595" Style="{StaticResource greyButtonTemplate}"/>
                    <Button x:Name="deleteButton" Content="DELETE TASK" HorizontalAlignment="Left" Margin="6,85,0,0" VerticalAlignment="Top" BorderBrush="#FFEE4747" Foreground="White" Width="180" FontSize="20" Background="#FFEE4747" Style="{StaticResource redButtonTemplate}"/>
                    <Image x:Name="retractButton" Margin="347,107,21,11" Source="/Assets/retract.png" Stretch="Fill" Tap="retractButton_Tap"/>
                </Grid>
            </StackPanel>
            <CheckBox x:Name="checkBox" IsChecked="{Binding IsComplete, Mode=TwoWay}" Content="" HorizontalAlignment="Left" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" Width="72" BorderThickness="0" Template="{StaticResource checkBoxTemplate}" Checked="checkBox_Checked" Unchecked="checkBox_Unchecked"/>
        </Grid>
    </DataTemplate>
但我无法访问此特定项目的itemGrid、taskTitle或taskDetail。我不知道如何将它们传递给tapTimer_Tick函数

我已经能够在元素上使用标记=“{binding itemID}”,但这仍然不允许我解决这个问题。 如何查找Tap源于的网格项2扩展,然后按名称访问同一网格中的元素

如果我想访问与单击相同的元素,那么很容易:

    private void taskTitle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        TextBox taskTitle = (TextBox)sender;

        taskTitle.IsEnabled = false;
    }

我一直在尝试如何使用Visual Tree Helper来解决这个问题,但我不知道如何解决。

了解MVVM。在过程代码中操作
DataTemplate
中的UI元素是有史以来最糟糕的想法。@HighCore我使用的是模型视图模型视图,但是我需要能够做到这一点,这样我才能向视图中添加动画。@HighCore如果不这样操作UI元素,那么有什么好办法呢?创建一个
UserControl
,并将相关的UI相关代码放在UserControl后面的代码中。然后把它放在
数据模板中
。我不知道这是否有帮助
但是试试这个
[[1]:
    private void taskTitle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        TextBox taskTitle = (TextBox)sender;

        taskTitle.IsEnabled = false;
    }