Question

I'm trying to work out how to Data Bind a list of UserControls to a WrapPanel but I'm not having much luck searching around.

I'm pretty new to WPF (come over from WinForms), at the moment I am adding the UserControls as children at runtime. Are there any solutions to this as I know WrapPanels don't support Data Binding.

Était-ce utile?

La solution

Try something like this:

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

If you don't need the selection stuff, you can try something with ItemsControl.

<ItemsControl ItemsControlScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding YourSource}">
     <ItemsControl.ItemsPanel>
           <ItemsPanelTemplate>
                   <WrapPanel IsItemsHost="True" />    
           </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     ...
</ItemsControl>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top