数据绑定-在运行时向Stackpanel添加控件(SL 4.0)



我有一个包含stackpanel的用户控件。

我正试图找到一种方法将stackpanel的孩子绑定到一个属性,以便我可以从另一个控件

访问它们

有人知道怎么做吗?

如果你正在使用DataBinding,那么启用一个绑定覆盖你的stackpanel与itemsControl。然后您可以使用名为ItemsProperty

的集合属性
<StackPanel>
   <ItemsControl x:Name="itemsControl" ItemsSource="{Binding ItemsProperty, Mode=TwoWay}" >
          <ItemsControl.ItemTemplate>
              <DataTemplate>
                 <ContentPresenter Content="{Binding}" /> <!-- you could also use Border -->
              </DataTemplate>
         </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

如果你在代码后面,你不需要重写ItemsControl,只需调用stackpanel1.Children.Add(item);(if x:Name="stackpanel1" in XAML).

然后stackPanel1。

最新更新