C# 将UserControls数据绑定到WrapPanel

C# 将UserControls数据绑定到WrapPanel,c#,wpf,data-binding,wrappanel,C#,Wpf,Data Binding,Wrappanel,我正在尝试解决如何将UserControls列表数据绑定到WrapPanel,但我在搜索时运气不太好 我是WPF的新手(来自WinForms),目前我正在运行时添加UserControls作为子控件。据我所知,WrapPanels不支持数据绑定,有什么解决方案吗。试试这样的方法: <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding YourSource}">

我正在尝试解决如何将UserControls列表数据绑定到WrapPanel,但我在搜索时运气不太好


我是WPF的新手(来自WinForms),目前我正在运行时添加UserControls作为子控件。据我所知,WrapPanels不支持数据绑定,有什么解决方案吗。

试试这样的方法:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding YourSource}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    ...
</ListBox>

...
如果不需要选择内容,可以尝试使用ItemsControl

<ItemsControl ItemsControlScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding YourSource}">
     <ItemsControl.ItemsPanel>
           <ItemsPanelTemplate>
                   <WrapPanel IsItemsHost="True" />    
           </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     ...
</ItemsControl>

...

尝试以下方法:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding YourSource}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    ...
</ListBox>

...
如果不需要选择内容,可以尝试使用ItemsControl

<ItemsControl ItemsControlScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding YourSource}">
     <ItemsControl.ItemsPanel>
           <ItemsPanelTemplate>
                   <WrapPanel IsItemsHost="True" />    
           </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     ...
</ItemsControl>

...