Disabling some checkboxes

4 posts, 0 answers
  1. eBuddy
    eBuddy avatar
    13 posts
    Member since:
    Jan 2012

    Posted 20 Mar 2012 Link to this post

    Hi,
    I need to prevent some checkboxes from being unchecked in list.
    I find those checkboxes by traversing visual tree, set IsEnabled=false. Their visual state changes to gray.
    But if i click checkbox - it becomes unchecked and visual state is like for normal state.

    This is a really strange bug. To my mind controls have to ignore interactions if they are disabled.
    Could you please have a look at that ?

    Thanks
  2. Deyan
    Admin
    Deyan avatar
    2041 posts

    Posted 21 Mar 2012 Link to this post

    Hi,

    The checkbox behavior withing the visual containers of RadDataBoundListBox is slightly different. Since visual containers are data bound and reused by the UI virtualization mechanism, directly setting properties of the visual elements inside the element tree of a container is not correct.

    If you wish to prevent an item from being checked/unchecked, you can use the ItemCheckedStateChanging event exposed by RadDataBoundListBox and cancel it in case you do not want the state of the item to change.

    If you also wish to make the checkbox within the container appear disabled, you can use the ItemStateChanged event and listen for the Realized and Recycled states in order to set the IsEnabled property of the checkbox and clear it to allow the container to be correctly reused for other items. The code could look like the following:

    void radDataBoundListBox_ItemStateChanged(object sender, Telerik.Windows.Controls.ItemStateChangedEventArgs e)
    {
        if (e.State == Telerik.Windows.Controls.ItemState.Realized)
        {
            RadDataBoundListBoxItem container = this.radDataBoundListBox.GetContainerForItem(e.DataItem) as RadDataBoundListBoxItem;
            ItemCheckBox checkBox = ElementTreeHelper.FindVisualDescendant<ItemCheckBox>(container);
            if (shouldDisable)
            {
                checkBox.IsEnabled = false;
            }
            else
            {
                checkBox.ClearValue(ItemCheckBox.IsEnabledProperty);
            }
        }
    }

    I hope this helps.

    Greetings,
    Deyan
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. DevCraft banner
  4. eBuddy
    eBuddy avatar
    13 posts
    Member since:
    Jan 2012

    Posted 21 Mar 2012 Link to this post

    Thanks for the tip with cancelling checking on ItemCheckedStateChanging event. It worked great.

    But using radDataBoundListBox_ItemStateChanged event handler you provided items looks disabled only first time. If the user try to check them, their visual state changes to "Pressed" and "Checked" regardless of IsEnabled property (with false value).
  5. Deyan
    Admin
    Deyan avatar
    2041 posts

    Posted 21 Mar 2012 Link to this post

    Hi,

    Thanks for writing back.

    I was able to reproduce this and can confirm that this is an issue which we are going to address. Since we release Internal Builds on weekly basis we will be able to include the fix for this case in our forthcoming IB and you will be able to download and use it from your account.

    Let me know in case you have further questions or need assistance.

    Kind regards,
    Deyan
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top