我正在尝试将自定义类的 observablecollection 绑定到简单的gridview。
这是网格视图模板:
<ListView Name="TestGrid" Grid.Row="1" Margin="0,10,0,0" ItemsSource="{Binding Path=testlist}">
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=AlertType}" Header="Alert" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Target}" Header="Target" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Message}" Header="Message" />
</GridView>
</ListView>
这是后面的代码:
public ObservableCollection<TestClass> testlist {get; set;}
public MainWindow()
{
InitializeComponent();
testlist = new ObservableCollection<TestClass>();
testlist.Add(new TestClass("Type1","Target1","Message1"));
testlist.Add(new TestClass("Type2", "Target2", "Message2"));
}
public class TestClass
{
public string AlertType { get; set; }
public string Target { get; set; }
public string Message { get; set; }
public TestClass(string a, string t, string m)
{
AlertType = a;
Target = t;
Message = m;
}
}
现在我一直在关注我在互联网上可以找到的样本。不幸的是,我在 gridview 中得到的唯一结果是显示以下消息的唯一行:“System.Windows.Controls.Columns.Count:3”
我在这里想念什么?