views:

159

answers:

1

Hi all, may you happy every day

I am new in silverlight

Suppose I am writing a user control(name as AAA.xaml) which contains a DataTemplate, in which I want to have another user control's(defined in BBB.xaml) instance by data binding.

I initilize a instance of BBB user control(name as bbb) in AAA.cs(the c# file of AAA.xaml), and I want something like this in the xaml of AAA:

<DataTemplate>
   <someKindOfControl SomeAttributeOfControl={Binding bbb} />
<DataTemplate>

Does it fesiable to show the BBB user control in the AAA or it is totally wrong? If it can work, how should I do to bind the user control instance properly? Which Control should I use?

+1  A: 

I might be misunderstanding your question, but you don't need to use binding to put an instance of one type of control into an instance of another type of control. I'd recommend making someKindOfControl derive from ContentControl, then you could do this:

<DataTemplate>
  <someKindOfControl>
    <bbb/>
  </someKindOfControl>
</DataTemplate>

Just make sure you use a ContentPresenter in your default style for someKindOfControl - that'll determine where bbb shows up.

On the other hand, if you have many controls that you want to insert into someKindOfControl you'd be best off using template parts to insert the controls and providing a style for someKindOfControl within the DataTemplate:

<DataTemplate>
  <someKindOfControl Style={StaticResource SomeKindOfStyle}/>
<DataTemplate>

Where SomeKindOfStyle provides a ControlTemplate that puts many types of custom control into various template parts of someKindOfControl:

<UserControl.Resources>
  <Style x:Name="SomeKindOfStyle" TargetType="myNamespace:someKindOfControl">
    <Setter Property="ControlTemplate">
      <Setter.Value>
        <ControlTemplate>
          <bbb x:Name="PART_TopRightControl/>
          <bbb x:Name="PART_BottomLeftControl/>
          <bbb x:Name="PART_CenterControl/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</UserControl.Resources>

That's a bit more complicated but can allow you to put multiple custom bbb controls into one instance of someKindOfControl. Keep in mind I'm not totally sure what you're after, but if you post a bit more info I might be able to clarify.

James Cadd