0

I have the following DataTemplate:

    <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>

Where the Grid item2Expanded is placed dynamically in a ListBox (Name="allToDoItemsListBox"). Text is added to each item via bindings.

The image retractButton has Tap="retractButton_Tap", As shown in the code:

    private void retractButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        if (isItemExpanded == true)
        {
            // Compacts current item
            itemGrid.Height = taskTitle.ActualHeight; // Restores itemGrid height to fit only taskTitle
            taskTitle.IsReadOnly = true; // taskTitle becomes only double-tap editable, single tap to expand once more

            taskDetail.IsHitTestVisible = false; // Stops overlapping taps
            isItemExpanded = false;
        }

        // Adds the event handler for single tap event
        tapTimer.Tick += new EventHandler(tapTimer_Tick);
        tapTimer.Start();
    }

    private void tapTimer_Tick(object sender, EventArgs e)
    {
        // Stop timer
        tapTimer.Tick -= new EventHandler(tapTimer_Tick);
        tapTimer.Stop();

        // Rest of the single tap function
        if (isItemExpanded == false)
        {
            taskDetail.IsHitTestVisible = true;
            taskDetail.IsEnabled = false;

            // Expands current item
            itemGrid.Height = double.NaN; // Sets itemGrid height to auto
            isItemExpanded = true;

            // Yeah... don't ask.
            // Stops temporary text highlighting/auto jumping to keyboard
            taskTitle.IsEnabled = false;
            taskTitle.IsEnabled = true;

            taskDetail.IsEnabled = true;
        }
    }

But I cannot access itemGrid, taskTitle, or taskDetail for this specific item. And I have no idea how to pass them to the tapTimer_Tick function.

I have been able to use the Tag="{binding itemID}" on elements, but that still hasn't allowed me to solve this issue. How do I find the grid item2Expanded that the Tap originated from, and then access elements in the same grid by name?

If I want to access the same element as was clicked, then it's easy:

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

        taskTitle.IsEnabled = false;
    }

I've been trying to work out how to use Visual Tree Helper to solve this problem, but I have no idea how to do it.

Seb123
  • 471
  • 1
  • 7
  • 20
  • Learn MVVM. Manipulating UI elements inside a `DataTemplate` in procedural code is the worst idea ever. – Federico Berasategui Aug 05 '13 at 17:25
  • @HighCore I am using Model View-model View, but I need to be able to do this so that I can add animations to the view. – Seb123 Aug 05 '13 at 17:35
  • @HighCore What would a good way be of going about it without manipulating UI elements like so? – Seb123 Aug 05 '13 at 21:17
  • Create a `UserControl` and put the relevant UI-related code in the code behind the UserControl. Then put that inside the `DataTemplate`. – Federico Berasategui Aug 05 '13 at 21:19
  • I no know if this you help
    But try this...
    [http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls][1] [1]: http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls
    – lucas_marquesm Sep 06 '13 at 14:19
  • I no know is this can help you. But, you can try it: [http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls][1] [1]: http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls – lucas_marquesm Sep 06 '13 at 17:10

0 Answers0