[英]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. 将ItemsSource设置为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. 看这行:
<ItemsControl ItemsSource="{Binding Problems}" >
在代码中,“问题”是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: 查看Visual Studio的输出窗口,您将看到以下内容:
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type;
System.Windows.Data错误:26:对于已经为ItemsControl的容器类型的项目,将忽略ItemTemplate和ItemTemplateSelector。 Type='XXX'
类型=“ 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: 我包装控件的意思是,您创建一个类,其中包含UserControl的属性,例如:
Problems : List<ProblemContainer>
public class ProblemContainer
{
public UserControl Problem { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.