简体   繁体   English

将DataTemplate的内容绑定到另一个userControl

[英]Bind the content of DataTemplate to another userControl

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. 假设我正在编写一个包含DataTemplate的用户控件(名称为AAA.xaml),在其中我想通过数据绑定具有另一个用户控件的实例(在BBB.xaml中定义)。

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: 我在AAA.cs(AAA.xaml的c#文件)中初始化BBB用户控件(名称为bbb)的实例,并且在AAA的xaml中需要这样的东西:

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

Does it fesiable to show the BBB user control in the AAA or it is totally wrong? 在AAA中显示BBB用户控件是否可行或完全错误? If it can work, how should I do to bind the user control instance properly? 如果可以,我应该如何正确绑定用户控件实例? Which Control should I use? 我应该使用哪个控件?

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: 我建议使someKindOfControl从ContentControl派生,那么您可以这样做:

<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. 只要确保您以某种默认样式将ContentPresenter用于someKindOfControl,即可确定bbb的显示位置。

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: 另一方面,如果您有许多要插入到someKindOfControl中的控件,则最好使用模板部件插入控件并在DataTemplate中为someKindOfControl提供样式:

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

Where SomeKindOfStyle provides a ControlTemplate that puts many types of custom control into various template parts of someKindOfControl: 其中SomeKindOfStyle提供了ControlTemplate,该模板将许多类型的自定义控件放入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. 这有点复杂,但是可以允许您将多个自定义bbb控件放入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. 请记住,我不确定您要做什么,但是如果您发布更多信息,我也许可以澄清。

You cannot bind to a user control in a data template. 您不能绑定到数据模板中的用户控件。

A data template is a type of user control. 数据模板是一种用户控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM