Skip to main content
 首页 » 编程设计

c#之使用绑定(bind)到 ListUserControl> 我该怎么做才能不显示控件

2024年02月23日12mfrbuaa

这是我正在处理的代码:

<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 时。它包含:

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

看看这一行:<ItemsControl ItemsSource="{Binding Problems}" > 在代码中,Problems 是一个 UserControls 列表。当我加载程序时,列表框显示来自用户控件的控件,它应该显示矩形。

我做错了什么?

请您参考如下方法:

查看 Visual Studio 的输出窗口,您将看到:

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

它不应用模板,因为可以直接添加项目。


关于包装控件,我的意思是创建一个类,其中包含 UserControl 的属性,例如:

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