自定义控件Xaml元素

时间:2011-05-04 10:45:59

标签: silverlight-4.0

我有

控件:

ControlX:IControlX和ControlY:IControlY

ControlZ具有ControlX的List属性

接口:

IControlX和IControlY:IControlX

我得到的标签是:

<ControlZ>

        <ControlY>

            <ControlX></ControlX>

             <ControlX></ControlX>

        </ControlY>

</ControlZ>

在这里,我可以访问列表中的ControlY但无法访问Control X。

但如果我将标签序列更改为:

<ControlZ>

        <ControlY> </ControlY>

        <ControlX></ControlX>

         <ControlX></ControlX>       

</ControlZ>

我可以获取列表中的所有对象。

但这不符合逻辑,所以我需要保持标签序列。

你能建议我吗?如何访问内部标记?

谢谢

2 个答案:

答案 0 :(得分:0)

你做不到。除非在XAML中初始化属性,否则XAML不允许您访问属性的属性。

<Control:MyControl>

  <Control:MyControl.Property1>

       <!-- Assuming that Property1 is of type MyOtherControl -->
       <Control:MyOtherControl Property="somevalue" />

 </Control:MyControl.Property1>

</Control:MyControl>

答案 1 :(得分:0)

要设置Property1属性的值,必须先将其设置为instanciate。假设Property1类型Property1Type存在于同一名称空间中,内部属性(Property)的类型是InnerPropertyType,它也在同一名称空间中。你的代码应该是这样的:

<Control:MyControl>
    <Control:MyControl.Property1>
        <Control:Property1Type>
            <Control:Property1Type.Property>
                <Control:InnerPropertyType />
            </Control:Property1Type.Property>
        </Control:Property1Type>
    </Control:MyControl.Property1>
</Control:MyControl>

这类似于,例如:

<ListBox>
  <ListBox.BorderBrush>
    <ImageBrush>
      <ImageBrush.Transform>
          <ScaleTransform ScaleX="5"/>
      </ImageBrush.Transform>
    </ImageBrush>
  </ListBox.BorderBrush>
</ListBox>

这应解决一些问题,如果其他问题仍然存在,请更新您的问题;)

希望这会有所帮助:)