简体   繁体   中英

Using binding to a List<UserControl> how can I do for not showing the controls

This is the code which I'm working:

<TextBlock TextWrapping="Wrap" Text="{Binding Objective}" Grid.Column="0" VerticalAlignment="Center" FontWeight="Bold" />
<ItemsControl ItemsSource="{Binding Problems}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle Stroke="Black" Height="20" Width="20" Margin="1,0" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>

When set ItemsSource to listBox. It contains:

List<Container>
(Below container properties)
    - Objective: string
    - Problems: List<UserControls>

Look at this line: <ItemsControl ItemsSource="{Binding Problems}" > In the code, Problems is a list of UserControls. When I load the program, the listbox is showing the controls from the user control and it's supposed to show the rectangle.

What am I doing wrong?

Look at the Output-window of Visual Studio and you will see this:

System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='XXX'

It does not apply the template as the Items can be added directly.


What i meant about wrapping your controls is that you create a class which holds a property for the UserControl, eg:

 Problems : List<ProblemContainer>
public class ProblemContainer
{
    public UserControl Problem { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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