2012-10-30 123 views
0

任何人都知道如何在重绘时在ListBox上引发事件。我试图掩盖有条件内容一列,但条件检查似乎因为没有什么掩盖的列表已被拉伸,所以面膜不工作之前完成:ListBox.Items更改后引发事件

/// <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); 
      } 
     } 
    } 

回答

0

我现在,问题在于当SearchTree函数被调用时没有绘制ListBox,所以从来没有任何DependencyObject传递给它。

我从一个LayoutUpdated事件放置标志代码说,检查已经完成,然后调用隐藏功能解决了这个问题(在我看来有点hackish的)

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