代码之家  ›  专栏  ›  技术社区  ›  Deniz Dogan

wpf:最简单的项控件将不显示任何项

  •  0
  • Deniz Dogan  · 技术社区  · 15 年前

    我不知道 ItemControl 工作。我只想填充我的 ItemsControl 一串 SomeItem .

    下面是代码:

    using System.Collections.ObjectModel;
    using System.Windows;
    
    namespace Hax
    {
    
        public partial class MainWindow : Window
        {
    
            public class SomeItem
            {
                public string Text { get; set; }
                public SomeItem(string text)
                {
                    Text = text;
                }
            }
    
            public ObservableCollection<SomeItem> Participants
                { get { return m_Participants; } set { m_Participants = value; } }
            private ObservableCollection<SomeItem> m_Participants
                = new ObservableCollection<SomeItem>();
    
            public MainWindow()
            {
                InitializeComponent();
                Participants.Add(new SomeItem("Hej!"));
                Participants.Add(new SomeItem("Tjenare"));
            }
        }
    }
    

    这是XAML:

    <ItemsControl Name="itemsParticipants"
        ItemsSource="{Binding Path=Participants}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Red" BorderThickness="2">
                    <TextBlock Text="{Binding Text}" />
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    我做错什么了?

    2 回复  |  直到 13 年前
        1
  •  1
  •   Guy Starbuck    15 年前

    确保将DataContext设置为XAML中的RelativeSource Self。如果您可以发布更多的XAML,可能会有所帮助。

    <ItemsControl... DataContext="{Binding RelativeSource={RelativeSource Self}}" >
    
        2
  •  1
  •   Thies    15 年前

    itemssource引用了一个名为myparticipants的属性,而它看起来是参与者的代码。

    检查“调试输出”视图,您应该看到错误消息。

    推荐文章