簡體   English   中英

使用綁定到列表 <UserControl> 我該怎么做才能不顯示控件

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

這是我正在工作的代碼:

<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}" >在代碼中,“問題”是UserControls的列表。 當我加載程序時,列表框會顯示用戶控件中的控件,並且應該顯示矩形。

我究竟做錯了什么?

查看Visual Studio的輸出窗口,您將看到以下內容:

System.Windows.Data錯誤:26:對於已經為ItemsControl的容器類型的項目,將忽略ItemTemplate和ItemTemplateSelector。 類型=“ XXX”

它不應用模板,因為可以直接添加項目。


我包裝控件的意思是,您創建一個類,其中包含UserControl的屬性,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM