Wpf 如何动态创建按钮列表并在ItemsControl中显示它们

Wpf 如何动态创建按钮列表并在ItemsControl中显示它们,wpf,Wpf,我有一个需要在按钮中加载的产品列表,但我无法使按钮显示 代码xaml: <ItemsControl ItemsSource="{Binding Path=Productos}" Grid.Row="4" Grid.Column="1" > <ItemsControl.Resources> <DataTemplate DataType="{x:Type local:SeleccionarOpcionesCot

我有一个需要在按钮中加载的产品列表,但我无法使按钮显示

代码xaml:

 <ItemsControl ItemsSource="{Binding Path=Productos}" Grid.Row="4"  Grid.Column="1" >
            <ItemsControl.Resources>
                <DataTemplate DataType="{x:Type local:SeleccionarOpcionesCotizacionesView2}">
                    <Button Command="{Binding CmdCuenta}" Style="{StaticResource TransparentStyle}"  Width="775" Height="157"/>
                </DataTemplate>
            </ItemsControl.Resources>

        </ItemsControl>

列表显示方式的图片(应显示按钮):

检查以下各项:

•确保名为“Productos”的财产是公共财产

•确保您使用的收集类型为“ObservableCollection”

•如果您使用的是MVVM模式,并且您的属性位于模型类中,请确保将您的模型设置为视图的上下文

 MyViewModel model = new MyViewModel();
 this.DataContext = model;
•确保ItemsControl实际可见。为测试目的,将高度和宽度设置为固定值

•如果您不使用MVVM,并且名为“Productos”的属性位于视图的分部类中,请确保在分部类上设置绑定。为此,请为控件/视图指定一个名称,并直接在视图控件元素上设置绑定

<UserControl x:Name="Control_View" />


<ItemsControl ItemsSource="{Binding Path=OpenUserProfileEditor, ElementName=Control_View}" />

阿洛斯,试着让你的Items控件像下面我的一样。(示例)



确保Productos在视图的DataContext中是公共属性。您可以直接将DataTemplate分配给ItemTemplate属性,而不将其添加到ItemsControl的资源中,并省略数据类型。要绑定到的
Productos
属性是如何定义的,以及在哪里定义的?除非该命令确实是数据项的属性,应该使用相对源将其绑定到主viewmodel(ItemsControl.DataContext)。
  <ItemsControl ItemsSource="{Binding Path=Productos}" Grid.Row="4"  Grid.Column="1" >
            <ItemsControl.ItemTemplate>
               <DataTemplate DataType="{x:Type local:SeleccionarOpcionesCotizacionesView2}">
                   <Button Command="{Binding CmdCuenta}" Style="{StaticResource TransparentStyle}"  Width="775" Height="157"/>
               </DataTemplate>
            </ItemsControl.ItemTemplate>
   </ItemsControl>