Checked and uncheked raised multiple times

6 posts, 1 answers
  1. Patrick
    Patrick avatar
    372 posts
    Member since:
    Aug 2012

    Posted 10 Oct 2012 Link to this post

    Hello,

    create a new Telerik Silverlight application and replace
    MainPage.xaml with:
    <UserControl
      x:Class="RadControlsSilverlightApp1.MainPage"
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
        <Grid Margin="5">
        <Grid.ColumnDefinitions>
          <ColumnDefinition />
          <ColumnDefinition />
        </Grid.ColumnDefinitions>
     
        <telerik:RadTreeView IsLineEnabled="True"
                             IsOptionElementsEnabled="True"
                             IsTriStateMode="True"
                             ItemsOptionListType="CheckList"
                             Margin="5"
                             Checked="RadTreeView_Checked"
                             Unchecked="RadTreeView_Unchecked">
          <telerik:RadTreeViewItem Header="Group"
                                   IsExpanded="True">
            <telerik:RadTreeViewItem Header="Item1" />
            <telerik:RadTreeViewItem Header="Item2" />
            <telerik:RadTreeViewItem Header="Item3" />
          </telerik:RadTreeViewItem>
        </telerik:RadTreeView>
     
        <telerik:RadListBox Name="EventList"
                            Margin="5"
                            Grid.Column="1" />
     
        </Grid>
    </UserControl>

    and
    MainPage.xaml.cs with:
    using System;
    using System.Windows.Controls;
    using Telerik.Windows;
     
    namespace RadControlsSilverlightApp1
    {
      public partial class MainPage : UserControl
      {
        public MainPage()
        {
          InitializeComponent();
        }
     
        private void RadTreeView_Checked(object sender, RadRoutedEventArgs e)
        {
          Dispatcher.BeginInvoke(
            () =>
              EventList.Items.Add(DateTime.Now.ToString() + ": Checked")
            );
        }
     
        private void RadTreeView_Unchecked(object sender, RadRoutedEventArgs e)
        {
          Dispatcher.BeginInvoke(
            () =>
              EventList.Items.Add(DateTime.Now.ToString() + ": Unchecked")
            );
        }
     
      }
    }

    Run the application:
    1. When you check Group, 4 Checked events are raised.
    2. Now if you uncheck Item1, one Checked and one Unchecked events are raised.
    3. Now if you uncheck Item2, one Unchecked event is raised: correct.
    4. Now if you check Item1, one Checked event is raised: correct.
    5. Now if you check Item2, two Checked events are raised.

    The Checked and Unchecked events should really be raised only once in cases 1 and 5.
    Case 2 is a special one, because the new state is indeterminate and there is no special event for this case.

    Patrick
  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 12 Oct 2012 Link to this post

    Hi Patrick,

    This is the expected behavior of the control. The additional events are invoked because the parent node needs to update it's status due to the TriStareMode. Please see the attached project for better understanding where the OriginalSource (the RadtreeViewItem for which is fired the event) is also written in the output.
    For further information you can also read about the TriStateMode in this article.

    Greetings,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Patrick
    Patrick avatar
    372 posts
    Member since:
    Aug 2012

    Posted 12 Oct 2012 Link to this post

    Pavel,
    the attached project is not the right one: it seems that it's a project about closable tab items.
    Patrick
  5. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 12 Oct 2012 Link to this post

    Hello Patrick,

    Please excuse me for the mistake I`ve made.

    Here is the correct project.

    Kind regards,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Patrick
    Patrick avatar
    372 posts
    Member since:
    Aug 2012

    Posted 12 Oct 2012 Link to this post

    Pavel,
    I see how I must correct my code to avoid firing my Changed event too often (I need to detect when there is any change is the selection).
    One last question: what is the difference between the IsChecked and CheckState properties of RadTreeViewItem?
    I've seen cases where IsChecked is false and CheckState is on?
    Patrick
  7. Answer
    Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1182 posts

    Posted 17 Oct 2012 Link to this post

    Hello Patrick,

    In brief, the RadTreeView uses the CheckState property to manage its TriStateMode while the IsChecked property is not used in tri-state value propagation as it is designed to be used with data binding. For more detailed information you can read this blog post.

    Kind regards,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner