C# 如何在listbox数据模板项中获取用户控件
我试图在列表框中添加用户控件,但找不到正确的方法来获取它 我使用下面的方法在绑定列表框中查找用户控件,但在10个项目中找不到3个或4个C# 如何在listbox数据模板项中获取用户控件,c#,windows-phone-8,C#,Windows Phone 8,我试图在列表框中添加用户控件,但找不到正确的方法来获取它 我使用下面的方法在绑定列表框中查找用户控件,但在10个项目中找不到3个或4个 private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject { var count = System.Windows.Media.VisualTreeHelper.Get
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = System.Windows.Media.VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = System.Windows.Media.VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
return (T)child;
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
return null;
}
private T findfirstelementvisualtree(DependencyObject parentElement),其中T:DependencyObject
{
var count=System.Windows.Media.visualtreeheloper.GetChildrenCount(parentElement);
如果(计数=0)
返回null;
for(int i=0;i
单击按钮查找用户控件的代码
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!downloadClicked)
{
downloadClicked = true;
SuraWithProgressBar prg = null;
ListBoxItem item = null;
for (int rowIndex = 0; rowIndex < lsbQuranData.Items.Count; rowIndex++)
{
item = this.lsbQuranData.ItemContainerGenerator.ContainerFromIndex(rowIndex) as ListBoxItem;
if (item != null)
{
prg = FindFirstElementInVisualTree<SuraWithProgressBar>(item);
if (prg != null)
{
//Do Somthing
prg.addButtonClickInterface(this);
}
}
}
}
else
MessageBox.Show("Please wait, downloading...");
}
private void按钮\u单击(对象发送者,路由目标)
{
如果(!downloadClicked)
{
downloadClicked=true;
SuraWithProgressBar prg=null;
ListBoxItem=null;
对于(int rowIndex=0;rowIndex
正如我在10项中提到的,它找不到3-4项。我正在寻找合适的方法在listbox中找到我的用户控件
谢谢 在经历了很多头痛之后,我通过以下两个链接找到了解决方案:
将模板添加到列表框中,然后将StackPanel更改为StackPanel,问题就解决了。请确保将此添加到ItemTemplate部分。在经历了很多麻烦之后,我在这里找到了解决方案()和()