C# 在ListBox之后引发事件。项已更改
任何人都知道如何在重新绘制列表框时在列表框上引发事件。我试图有条件地屏蔽一列中的内容,但条件检查似乎是在绘制列表框之前完成的,因此屏蔽不起作用,因为没有任何内容可屏蔽:C# 在ListBox之后引发事件。项已更改,c#,windows-phone-7,listbox,C#,Windows Phone 7,Listbox,任何人都知道如何在重新绘制列表框时在列表框上引发事件。我试图有条件地屏蔽一列中的内容,但条件检查似乎是在绘制列表框之前完成的,因此屏蔽不起作用,因为没有任何内容可屏蔽: /// <summary> /// Locks or unlocks the quantity textbox based on 100% flour and activates or deactivate weights /// </summary> private vo
/// <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);
}
}
}
//
///基于100%面粉锁定或解锁数量文本框,并激活或禁用权重
///
私有void activatePieceQuantity()
{
如果(IsPercent())
{
((TextBox)NumberOfItemsTextBox作为TextBox)。IsEnabled=true;
权重主动(真);
}
其他的
{
((TextBox)NumberOfItemsTextBox作为TextBox)。IsEnabled=false;
权重主动(假);
}
}
///
///发送控件以使用控件名称和激活或停用标志进行搜索
///
///
私有无效权重活动(布尔激活)
{
int locationInList=0;
foreach(活性配方中的配方成分ri。配方成分)
{
SearchTree(this.IngreditsListBox.ItemContainerGenerator.ContainerFromIndex(locationInList),“QuanityWeight”,激活);
locationInList++;
}
}
///
///在成分列表中查找所有与重量相关的对象,并相应地设置可见性
///
///
///从文本框的标记派生
///
私有void SearchTree(DependencyObject targetElement、字符串flagName、bool enableFlag)
{
if(targetElement==null)
返回;
变量计数=VisualTreeHelper.GetChildrenCount(targetElement);
如果(计数=0)
返回;
for(int i=0;i
我现在知道了,问题是调用SearchTree函数时没有绘制ListBox,因此没有任何DependencyObject传递给它
我通过在代码中放置一个标志来表示检查已经完成,然后从LayoutUpdated事件调用掩蔽函数,解决了这个问题(我认为这有点黑客化)
private void IngredientsListBox_LayoutUpdated(object sender, EventArgs e)
{
if (ingredientsListLoaded)
{
activatePieceQuantity();
ingredientsListLoaded = false;
}
}