使用绑定到 List<UserControl>我该怎么做才能不显示控件 [英] 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>
当将 ItemsSource 设置为 listBox 时.它包含:
When set ItemsSource to listBox. It contains:
List<Container>
(Below container properties)
- Objective: string
- Problems: List<UserControls>
看这一行:<ItemsControl ItemsSource="{Binding Problems}" >
在代码中,Problems 是一个 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.
我做错了什么?
推荐答案
查看 Visual Studio 的输出窗口,您会看到:
Look at the Output-window of Visual Studio and you will see this:
System.Windows.Data 错误:26:ItemTemplate 和 ItemTemplateSelector 对于已经属于 ItemsControl 的容器类型的项目被忽略;Type='XXX'
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.
我关于包装控件的意思是您创建一个类来保存 UserControl 的属性,例如:
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; }
}
这篇关于使用绑定到 List<UserControl>我该怎么做才能不显示控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!