简体   繁体   English

在ListBox.Items更改后引发事件

[英]Raise an event after ListBox.Items has changed

anyone know how to raise an event on a ListBox when its redrawn. 任何人都知道如何在重绘ListBox时引发事件。 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. 我现在知道了,问题是调用SearchTree函数时没有绘制ListBox,所以从没有任何DependencyObject传递给它。

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 我通过在代码中放置一个标记以表明检查已完成,然后从LayoutUpdated事件中调用masking函数,解决了该问题(我认为有些黑)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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