views:

556

answers:

1

Hi I am creating a listbox containing 2 text boxes each time a user clicks on a button (example controls to demo) and adding them to the stackpanel defined in the XAML.

<StackPanel Name="myStackPanel">
    <Button Name="myButton" Click="myButton_Click">Click to add a new button</Button>
</StackPanel> 

    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        TextBox tb = new TextBox();
        TextBox tb2 = new TextBox();

        tb.Text = "Some text 1";
        tb2.Text = "Some text 2";

        ListView lv = new ListView();
        lv.Name = "anotherLv";
        lv.Items.Add(tb);
        lv.Items.Add(tb2);

        this.myStackPanel.Children.Add(lv);
    }

This works fine.

I need the ability to start a timer when each listview is created that will count down from 5 mins to 0. Once the timer hits 0 the listview associated with that timer will be removed from the screen. The timer will need to be reset back to 5 mins each time their is some activity in the associated listbox (ie some text is typed in, or a mouseover occurs).

As the user has the ability to create any number of these listviews i will need to create a seperate timer for each listview created and then monitor the events for that specific listview and contained controls.

I have a few points i am srugaling to get started with.

1) How will i attach the timer to each listview?
2) How will i monitor the events for each listview and other controls within it to reset the associated timer?

I hope this all makes sense and someone has some pointers for me.

Thanks.

A: 

How will i attach the timer to each listview?

You could store the timers in a Dictionary<ListViewItem, DispatcherTimer>. That is, a map from the ListViewItem to the associated DispatcherTimer.

How will i monitor the events for each listview and other controls within it to reset the associated timer?

You create the items, so you can attach events handlers according to your requirements. For example:

ListView lv = new ListView();
lv.Name = "anotherLv";
lv.Items.Add(tb);
lv.Items.Add(tb2);

tb.TextChanged += ResetTimer;
tb2.TextChanged += ResetTimer;

private void ResetTimer(object sender, RoutedEventArgs e)
{
    //reset timer here
}

That said, I'd recommend you encapsulate all this logic in a view model instead of trying to do everything directly in the view.

HTH, Kent

Kent Boogaart
Thanks Kent, exactly what i needed the DispatcherTimer.I am still a little unclear about the naming of the listview.In the example we are naming it “anotherLv” each time a new one is added.Should i be dynamically naming the control so “anotherLv” + timestamp before adding the listview to the dictionary so in the tick event i can differentiate the controls.I guess what i am asking is how i will reference the correct listview in the ResetTimer event as all the TextChanged events wired up to the different sets of texboxes will call the same method.
Jenine Burg
OK, maybe swap the dictionary around. In other words, map DispatcherTimer to corresponding ListViewItems. So in your event handler, the DispatcherTimer is the sender argument, so you can use that to look up the corresponding ListViewItem.
Kent Boogaart