Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > Windows Presentation Foundation
|
Windows Presentation Foundation Discussion of the beta version of Windows Presentation Foundation "WPF" formerly know as codename "Avalon" as well as the related XAML.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Windows Presentation Foundation section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 22nd, 2007, 05:48 PM
Registered User
 
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default CheckBox binding in ListView - Multiple events!

I'm binding a checkbox in a ListView to a boolean column in a DataTable, and catching the Checked & Unchekced events to add & remove rows from the table (this is to create a multi-select, multi-column TreeListView, because the WPF TreeView is so basic).

Here's the strange behavior:

When I handle the Checked event and insert rows, all is well. But after I remove the rows in the Unchecked handler, the next time I click on any CheckBox, I get multiple Checked events, one for each time I handled the Unchecked event! The events are not nested so I can't use a flag to block them.

Any ideas? Here's a code snippet for the Unchecked event handler:

// Remove child rows (rows after current row with Level greater than current row)
private void OnUnChecked(object sender, RoutedEventArgs e)
{
    e.Handled = true;
    DataRow row = ((DataRowView)((CheckBox)sender).DataContext).Row;

    List<DataRow> rowsToRemove = new List<DataRow>();
    for (int i = table.Rows.IndexOf(row) + 1; i < table.Rows.Count; i++)
    {
        if ((int)table.Rows[i]["Level"] <= (int)row["Level"])
        {
            break;
        }

        rowsToRemove.Add(table.Rows[i]);
    }

    foreach (DataRow rowToRemove in rowsToRemove)
    {
        table.Rows.Remove(rowToRemove);
    }
}


 
Old April 22nd, 2007, 06:39 PM
Registered User
 
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As a workaround, if I handle the Click event for the CheckBox instead of the Checked & Unchecked events, I don't have this problem.



This would appear to be a bug in the ListView - perhaps it is reusing ListViewItems and forgetting to remove Checked event handlers? I forgot to mention that I tried rebinding to the table after removing rows in order to start fresh, but I still got multiple Checked events.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding ComboBox inside ListView problem gunnjamie Windows Presentation Foundation 1 July 17th, 2008 02:13 PM
binding a checkbox ?? msordo VB.NET 2002/2003 Basics 0 April 9th, 2008 05:35 PM
Binding a checkbox that contain Y/N values aad1 C# 1 May 17th, 2006 07:23 PM
Binding a comboBox to update a listView Amethyst1984 VB How-To 2 February 28th, 2006 03:07 PM
Adding a checkbox into a listview?? pauljoy Pro VB 6 0 January 9th, 2006 08:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.