Infragistics Home

Infragistics Forums

Infragistics community online discussions.
Welcome to Infragistics Forums Sign in | FAQ
in Search

Disable checkbox in UltraListViewItem?

Last post 07-29-2008 9:54 by [Infragistics] Brian Fallon. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 06-10-2008 12:42

    Disable checkbox in UltraListViewItem?

    I would like to be able to disable the checkbox for specific UltraListViewItem instances.  I only want the checkbox to appear disabled (ie: greyed out) not the item itself because I still want to be able to select the item.  I want to disable the items as I'm creating them and adding them to the UltraListView.  So far the only way I can the checkboxes disabled is on the ItemCheckStateChanging event by looping through like this:

             foreach (UltraListViewItem item in ultraListView.Items)
             {
                foreach (Infragistics.Win.UIElement child in item.UIElement.ChildElements)
                {
                   if (child is UltraListViewItemCheckBoxUIElement)
                   {
                      UltraListViewItemCheckBoxUIElement check = child as UltraListViewItemCheckBoxUIElement;
                      check.Enabled = false;
                   }
                }
             }

    This works, but obviously is not happening when I want it to.  I would like to do this much earlier (when I'm originally creating these items), however the UIElement is always null at that time.

    Is there any way I can do this?

    • Post Points: 20
  • 06-11-2008 9:56 In reply to

    Re: Disable checkbox in UltraListViewItem?

    Answer

    You can use the IUIElementCreationFilter interface to accomplish this:

    this.listView.CreationFilter = new DisabledCheckBoxCreationFilter();


    private class DisabledCheckBoxCreationFilter : IUIElementCreationFilter
    {
        #region IUIElementCreationFilter Members

        void IUIElementCreationFilter.AfterCreateChildElements(UIElement parent)
        {
            if ( parent is UltraListViewItemUIElement )
            {
                CheckBoxUIElement checkBoxElement = parent.GetDescendant( typeof(CheckBoxUIElement) ) as CheckBoxUIElement;
                if ( checkBoxElement != null )
                    checkBoxElement.Enabled = false;
            }
        }

        bool IUIElementCreationFilter.BeforeCreateChildElements(UIElement parent)
        {
            return false;
        }

        #endregion
    }

    • Post Points: 50
  • 07-15-2008 12:38 In reply to

    • k3n51mm
    • Not Ranked
    • Joined on 07-07-2008
    • Points 140

    Re: Disable checkbox in UltraListViewItem?

    We also have this requirement: disable the checkbox while allowing item selection.

    We have it working mostly, but we've recently run into a situation where it breaks, because the UIElement

    evaluates as Nothing. We don't really understand the UIElement, so we can't figure out why it's Nothing

    but only under certain circumstances. The snippet below is our current code, which was working for awhile.

    I thought I remembered getting the code off this forum, but looking at the solution above, it seems completely different.

    Could you explain the difference, and provide a VB version of the code above, if that's what we should be using instead?

    Thanks in advance.

     

    '--------------------------------------------------- BEGIN CODE

                       For Each item As UltraListViewItem In ulvSample.Items
                            If item.Text = SampleID Then
                                'this was found in other listview, so disable its checkbox
                                For Each child As Infragistics.Win.UIElement In item.UIElement.ChildElements
                                    If TypeOf child Is UltraListViewItemCheckBoxUIElement Then
                                        Dim check As UltraListViewItemCheckBoxUIElement = TryCast(child, UltraListViewItemCheckBoxUIElement)
                                        check.Enabled = False
                                    End If
                                Next child
                            Else
                                'this was not found in other listview, so enable its checkbox
                                For Each child As Infragistics.Win.UIElement In item.UIElement.ChildElements
                                    If TypeOf child Is UltraListViewItemCheckBoxUIElement Then
                                        Dim check As UltraListViewItemCheckBoxUIElement = TryCast(child, UltraListViewItemCheckBoxUIElement)
                                        check.Enabled = True
                                    End If
                                Next child
                            End If
                        Next item

     '---------------------------------------------------END CODE

    • Post Points: 20
  • 07-16-2008 9:21 In reply to

    Re: Disable checkbox in UltraListViewItem?

    Just insert a null reference check before accessing the UIElement property, i.e., "If Not item.UIElement Is Nothing Then"

    • Post Points: 20
  • 07-16-2008 12:46 In reply to

    • k3n51mm
    • Not Ranked
    • Joined on 07-07-2008
    • Points 140

    Re: Disable checkbox in UltraListViewItem?

    Yes of course we were able to do that, but that's not what we need. We need to disable the checkbox. We cannot understand why this occurs intermittently. What it comes down to is:

    If Not item.UIElement Is Nothing Then

          DisableTheCheckbox()

    Else

          WeStillNeedToDisableTheCheckbox()

    End If

    If UIElement is evaluating to Nothing, then that doesn't change the fact that we need to disable the checkbox. And, as far as we know, the only way to do that is iterate through the child elements of the UIElement, find it and disable it.

    If there is no UIElement, why? And if it doesn't exist, how can we create one with which we can achieve our objective?

    Of course if there is another simpler method of disabling checkboxes per item in the WinListView, we're more than happy to use a more correct method.

     Thanks

     

    • Post Points: 20
  • 07-17-2008 11:06 In reply to

    Re: Disable checkbox in UltraListViewItem?

    The UIElement property returns null when the corresponding item is not contained within the viewable area of the control.
    Your approach will only work when all items are contained within the viewable area of the control.

    The following code sample is the VB equivalent of the C# code I posted on 6/11/08, which uses the IUIElementCreationFilter interface to solve the problem of disabling checkboxes:


    Public Class DisabledCheckBoxCreationFilter
        Implements IUIElementCreationFilter

        Public Sub AfterCreateChildElements(ByVal parent As Infragistics.Win.UIElement) Implements Infragistics.Win.IUIElementCreationFilter.AfterCreateChildElements
            If Not parent Is Nothing AndAlso parent.GetType() Is GetType(UltraListViewItemUIElement) Then
                Dim checkboxElement As CheckBoxUIElement = parent.GetDescendant(GetType(CheckBoxUIElement))
                If Not checkboxElement Is Nothing Then
                    checkboxElement.Enabled = False
                End If
            End If
        End Sub

        Public Function BeforeCreateChildElements(ByVal parent As Infragistics.Win.UIElement) As Boolean Implements Infragistics.Win.IUIElementCreationFilter.BeforeCreateChildElements
            Return False
        End Function
    End Class

    • Post Points: 20
  • 07-24-2008 18:54 In reply to

    • k3n51mm
    • Not Ranked
    • Joined on 07-07-2008
    • Points 140

    Re: Disable checkbox in UltraListViewItem?

     Thanks for the response Brian. How exactly would I implement this, such as adding it to the form in question, executing it from the appropriate event, and calling the correct code when the elements are scrolled into view?

    • Post Points: 20
  • 07-25-2008 10:18 In reply to

    Re: Disable checkbox in UltraListViewItem?

    assign an instance of this class to the control's CreationFilter property

    • Post Points: 20
  • 07-25-2008 12:44 In reply to

    • k3n51mm
    • Not Ranked
    • Joined on 07-07-2008
    • Points 140

    Re: Disable checkbox in UltraListViewItem?

     Thanks Brian, that's pretty awesome, when we assign the instance it disables all the checkboxes in the control. But, either we didn't understand your answer, or that's not quite what we were looking for. Here's the overarching idea:

    If the user has Checked an item's box, then they must not be allowed to Select it. (This is working OK)

    Conversely, if the user has Selected an item, (and thereby adding its value to another listview), they must not be allowed to also Check its box.    (This is what we're working on now)

     What we need is a method to exclusively disable/reenable any checkbox at will. Is there a way we can use your class to disable the Checkboxes only in certain items... and then also be able to reenable them if needed? That's what we're looking for.

     We didn't see a CreationFilter property at the ListViewItem level, and didn't see a clear way to change the class to allow another condition, such as a Tag setting on each ListViewItem. It seems the Class operates at the UIElement level, whose parent is always a ListViewItemUIElement (??). If we could reference each ListViewItem from within the Class, then we could control which ListViewItems' checkboxes are disabled and which are not.

    If this is not how you intended it to be used, please feel free to correct us.

    Thanks

    • Post Points: 20
  • 07-29-2008 9:54 In reply to

    Re: Disable checkbox in UltraListViewItem?

    The UIElement class exposes a 'GetContext' method. If you call this method off the CheckBoxUIElement, passing in typeof(UltraListViewItem), you will get a reference to the associated UltraListViewItem. Once you have that reference you can use UltraListView.SelectedItems.Contains to determine whether that item is selected.

    • Post Points: 5
Page 1 of 1 (10 items)
Powered by Community Server (Commercial Edition), by Telligent Systems