我有一个嵌套的ItemsControl
来显示以下模型:
public class Parent
{
public string ParentTitle
{
get;
set;
}
ICollection<Child> Children
{
get;
set;
}
}
public class Child
{
public string ChildTitle
{
get;
set;
}
}
ItemsControl
看起来像这样:
<ItemsControl x:Name="listOfParents">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:Parent}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button x:Name="btnTarget" Grid.Row="0" Content="{Binding ParentTitle}"></Button>
<ItemsControl Grid.Row="1" ItemsSource="{Binding Children}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:Child}">
<Button x:Name="btnSource" Content="{Binding ChildTitle}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
listOfParents Itemssouce是List<Parent>
。单击 btnSource 时,如何访问Button
btnTarget ?
答案 0 :(得分:1)
您可以使用Button
FindChild()
功能列表:
public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
{
if (parent == null)
{
return null;
}
T foundChild = null;
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
T childType = child as T;
if (childType == null)
{
foundChild = FindChild<T>(child, childName);
if (foundChild != null) break;
}
else
if (!string.IsNullOrEmpty(childName))
{
var frameworkElement = child as FrameworkElement;
if (frameworkElement != null && frameworkElement.Name == childName)
{
foundChild = (T)child;
break;
}
else
{
foundChild = FindChild<T>(child, childName);
if (foundChild != null)
{
break;
}
}
}
else
{
foundChild = (T)child;
break;
}
}
return foundChild;
}
拨打电话:
private void btnSource_Click(object sender, RoutedEventArgs e)
{
Button MyBtnTarget = FindChild<Button>(listOfParents, "btnTarget");
MessageBox.Show(MyBtnTarget.Content.ToString());
}
但是这样,该函数将选择第一个按钮,我们需要访问所有元素。为此,我重写了该函数,以便返回列表的所有元素。这是代码:
public static void FindChildGroup<T>(DependencyObject parent, string childName, ref List<T> list) where T : DependencyObject
{
// Checks should be made, but preferably one time before calling.
// And here it is assumed that the programmer has taken into
// account all of these conditions and checks are not needed.
//if ((parent == null) || (childName == null) || (<Type T is not inheritable from FrameworkElement>))
//{
// return;
//}
int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++)
{
// Get the child
var child = VisualTreeHelper.GetChild(parent, i);
// Compare on conformity the type
T child_Test = child as T;
// Not compare - go next
if (child_Test == null)
{
// Go the deep
FindChildGroup<T>(child, childName, ref list);
}
else
{
// If match, then check the name of the item
FrameworkElement child_Element = child_Test as FrameworkElement;
if (child_Element.Name == childName)
{
// Found
list.Add(child_Test);
}
// We are looking for further, perhaps there are
// children with the same name
FindChildGroup<T>(child, childName, ref list);
}
}
return;
}
}
通话功能:
private void btnSource_Click(object sender, RoutedEventArgs e)
{
// Create the List of Button
List<Button> list = new List<Button>();
// Find all elements
FindChildGroup<Button>(listOfParents, "btnTarget", ref list);
string text = "";
foreach (Button elem in list)
{
text += elem.Content.ToString() + "\n";
}
MessageBox.Show(text, "Text in Button");
}
通常,有几种方法可以访问模板。这是一个:How to use FindName with a ContentControl。