C# 在xaml中读取子标记(类似于列表框';s)

C# 在xaml中读取子标记(类似于列表框';s),c#,silverlight,xaml,listbox,controls,C#,Silverlight,Xaml,Listbox,Controls,在Silverlight中,可以通过observablecollection类或类似的xaml来填充列表框: <TextBox Name="tb" Text="DroplistBox" Width="140" Height="30"></TextBox> <ListBox Name="lb" Width="150" Height="200" SelectionMode="Single"> <ListB

在Silverlight中,可以通过observablecollection类或类似的xaml来填充列表框:

        <TextBox Name="tb" Text="DroplistBox" Width="140" Height="30"></TextBox>
        <ListBox Name="lb" Width="150" Height="200" SelectionMode="Single">
           <ListBoxItem>
                <TextBox Text="1" Width="140" Height="30"></TextBox>
            </ListBoxItem>
            <ListBoxItem>
                <TextBox Text="2" Width="140" Height="30"></TextBox>
            </ListBoxItem>
            <ListBoxItem>
                <TextBox Text="3" Width="140" Height="30"></TextBox>
            </ListBoxItem>
            <ListBoxItem>
                <TextBox Text="4" Width="140" Height="30"></TextBox>
            </ListBoxItem>
        </ListBox>

所以我的问题是,我怎样才能在自己的控制下复制它?所以基本上,我不确定如何解析ListBox中的ListBoxItem。tehre是我应该寻找的一种列表FindChildren(字符串名)类型的函数吗

我在福森回答的帮助下解决了这个问题,以下是我所做的要点,如果有人感兴趣,因为我无法在对福森回答的评论中非常清楚地解释自己:

   [ContentProperty( "Items" )]
   public class ControlWithChildReader : Control
   {

      List<object> m_objects = new List<object>( );
      public object Items
      {
         get
         {
            return m_objects[ 0 ]; //probably not needed haha
         }
         set
         {
            m_objects.Add( value );

         }
      }
[ContentProperty(“项目”)]
公共类控件WithChildReader:控件
{
List m_objects=新列表();
公共物品
{
得到
{
返回m_对象[0];//可能不需要哈哈
}
设置
{
m_对象。添加(值);
}
}

< /代码> 您可以通过继承继承该函数,然后通过属性访问该子。

您可以通过继承继承该函数,然后通过属性访问该子。

这可能有助于处理控件内的模板和控件,请考虑此XAML:

XAML


<>这可能有助于处理控件内部的模板和控件,请考虑此XAML:

XAML


通过在集合上放置以下属性,可以复制ItemsControl exibits的行为

[ContentProperty(“Items”,true)]


这将向Items集合属性添加XAML内容。

您可以通过在集合上放置以下属性来复制ItemsControl exibits的行为

[ContentProperty(“Items”,true)]


这将向Items集合属性添加XAML内容。

Ah这有点帮助,我添加了ContentProperty(“Items”)我的类的一个属性叫做TextBox Items,但我只能用这种方式从xaml中读取一个TextBox。是否有一种方法可以读取所有TextBox子项?Nevermind通过让属性将设置好的任何内容添加到列表中来解决这个问题:DAh这有点帮助,我添加了ContentProperty(“Items”)我的类有一个名为TextBox Items的属性,但我只能通过这种方式从xaml中读取一个TextBox。是否有一种方法可以读取所有TextBox子项?通过让属性将设置为它的任何内容添加到列表中,Nevermind解决了这一问题:D
    <ListBox Name="lb" Width="150" Height="200" SelectionMode="Single">
       <ListBoxItem>
            <TextBox Text="1" Width="140" Height="30"></TextBox>
        </ListBoxItem>
        <ListBoxItem>
            <TextBox Text="2" Width="140" Height="30"></TextBox>
        </ListBoxItem>
        <ListBoxItem>
            <TextBox Text="3" Width="140" Height="30"></TextBox>
        </ListBoxItem>
        <ListBoxItem>
            <TextBox Text="4" Width="140" Height="30"></TextBox>
        </ListBoxItem>
    </ListBox>
ListBoxItem item1 = lb.SelectedItem;
Textbox tb1 = item1.GetVisualDescendants().OfType<TextBox>().FirstOrDefault() as TextBox;
List<Textbox> tbCollection = lb.GetVisualDescendants().OfType<TextBox>().ToList();
GetVisualAncestors(); - Gets all anscestor controls(parent of parent of the item)
GetVisualChildren(); - Gets all controls inside a controls which are in the control
GetVisualSiblings(); - Gets all controls that share the same parent with the control