1

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?

Darf
  • 2,495
  • 5
  • 26
  • 37

1 Answers1

4

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, e.g.:

 Problems : List<ProblemContainer>
public class ProblemContainer
{
    public UserControl Problem { get; set; }
}
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • 1
    @OscarFimbres: Yes, because it *can*, the ItemTemplate is ignored. Also it's not a ListBox, a ListBox would **not** show them.. – H.B. Sep 03 '11 at 18:52
  • Sorry for my basic knowledge, but how can I fix it? – Darf Sep 03 '11 at 18:54
  • @OscarFimbres: Wrap the `UserControls` in a class for example, that way the ItemsControl will not "see" them. – H.B. Sep 03 '11 at 18:56
  • @OscarFimbres: It should show the template, don't you see empty rectangles? – H.B. Sep 03 '11 at 19:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3133/discussion-between-oscar-fimbres-and-h-b) – Darf Sep 03 '11 at 19:38
  • @OscarFimbres: You should modify a lot of code anyway because your data is not really supposed to hold on to instances of UI-elements such as those UserControls. As i see it the design was rather flawed to begin with (see also [model-view-controler](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller), a related pattern, if you do not know that) – H.B. Sep 03 '11 at 22:10
  • I'm still reading about that pattern, is so interesting.. but I guess I understand the concepts.. anyway, will you provide a little of help on this? – Darf Sep 04 '11 at 21:40
  • @OscarFimbres: Sorry, i just answer questions (if i feel like it), and preferably with as little of a "tail" as possible. This site is not for support and it is not a forum either, If you have any specific problem, search first and possibly ask a question if nothing comes up, and see if **anyone** can help you. – H.B. Sep 04 '11 at 22:29
  • I was investigating about the MVC pattern, but I see many examples in internet about MVVM. What do you think.. is better do the patten in MVVM for this case? – Darf Sep 05 '11 at 04:33
  • @OscarFimbres: Use MVVM, it's a pattern more specific to WPF. – H.B. Sep 05 '11 at 11:28