简体   繁体   中英

Raise an event after ListBox.Items has changed

anyone know how to raise an event on a ListBox when its redrawn. I'm trying to conditionally mask content in one column but the conditional check seems to be done before the listbox has been drawn and so the mask does not work because there is nothing to mask:

    /// <summary>
    /// Locks or unlocks the quantity textbox based on 100% flour and activates or deactivate weights
    /// </summary>
    private void activatePieceQuantity()
    {
        if (isFlour100Percent())
        {
            ((TextBox)NumberOfItemsTextBox as TextBox).IsEnabled = true;
            weightsActive(true);
        }
        else
        {
            ((TextBox)NumberOfItemsTextBox as TextBox).IsEnabled = false;
            weightsActive(false);
        }
    }

    /// <summary>
    /// Send controls to search with control name and activate or deactivate flag
    /// </summary>
    /// <param name="activate"></param>
    private void weightsActive(bool activate)
    {
        int locationInList = 0;
        foreach (RecipieIngredient ri in activeRecipie.RecipieIngredients)
        {
            SearchTree(this.IngredientsListBox.ItemContainerGenerator.ContainerFromIndex(locationInList), "QuanityWeight", activate);
            locationInList++;
        }
    }

    /// <summary>
    /// Find all weight related objects in the ingredients list and set the visibility accordingly
    /// </summary>
    /// <param name="targetElement"></param>
    /// <param name="flagName">Derived from the Tag of the textbox</param>
    /// <param name="enableFlag"></param>
    private void SearchTree(DependencyObject targetElement, string flagName, bool enableFlag)
    {
        if (targetElement == null)
            return;
        var count = VisualTreeHelper.GetChildrenCount(targetElement);
        if (count == 0)
            return;

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(targetElement, i);
            if (child is TextBlock)
            {
                TextBlock targetItem = (TextBlock)child;

                if (targetItem.Name == flagName)
                    if (enableFlag)
                    {
                        ((TextBlock)targetItem as TextBlock).Visibility = Visibility.Visible;
                        return;
                    }
                    else
                    {
                        ((TextBlock)targetItem as TextBlock).Visibility = Visibility.Collapsed;
                    }
            }
            else
            {
                SearchTree(child, flagName, enableFlag);
            }
        }
    }

I got it now, the problem was that the ListBox was not drawn when the SearchTree function was called so there was never any DependencyObject to pass to it.

I solved the problem (somewhat hackish in my opinion) by placing a flag in the code to say that the check had been done and then calling the masking function from a LayoutUpdated event

    private void IngredientsListBox_LayoutUpdated(object sender, EventArgs e)
    {
        if (ingredientsListLoaded)
        {
            activatePieceQuantity();
            ingredientsListLoaded = false;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM