private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
var c = VisualTreeHelper.GetChildrenCount(depObj);
if (c == 36)
{
Trace.WriteLine(c);
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
这个函数遍历的数量与ListBox长度不一致。
出现这个问题的原因是Listbox控件还没有完成可视化,并且 UI Virtualization 功能正在生效。在使用 UI Virtualization 功能的时候,ListBox 只会把可见的项进行实际的可视化处理,对于屏幕外的项,并不会创建对应的元素。因此,如果调用 ItemContainerGenerator.ContainerFromIndex(index) 方法时,该项元素还没有被实际创建,返回值会为 null。
有两种解决办法:
1.滚动到所有项,再使用FindVisualChildren函数,当然,为了减少遍历次数,也可以间隔滚动。
for(int i =0;i < ListBox.Items.Count;i++)
{
object item = ListBox.Items[index];
ListBox.ScrollIntoView(item);
};
foreach (System.Windows.Controls.Button btn in FindVisualChildren<System.Windows.Controls.Button>(ListBox))
{
btn.IsEnabled= false;
}
2.通过绑定的方式(推荐)
ObservableCollection<DataListClass> DataList= new ObservableCollection<DataListClass>();
ListBox.ItemsSource = DataList;
foreach (var Data in DataList)
{
Data.IsEnabled = false;
}
//-----------------------class--------------------------
public class DataListClass: INotifyPropertyChanged
{
bool _isenabled ;
public bool IsEnabled
{
get { return _isenabled ; }
set { _isenabled = value; FirePropertyChanged("IsEnabled "); }
}
public virtual event PropertyChangedEventHandler PropertyChanged;
public virtual void FirePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
xaml:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<Button IsEnabled="{Binding IsEnabled}">
</Button>
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>