我一直在尝试这一整天,但没有任何运气。我能够显示UserControl,但我无法弄清楚为什么列将不会显示,即使我已将它们设置在XAML中。此外,我不确定数据是否显示,因为表单空白。ListView不会在运行时更新
这是我从其他程序调用的类库的一部分。但是,它总是显示一个空白表格并且没有任何更新。
下面是我的UserControl代码。
public partial class DisplayData : UserControl
{
ObservableCollection<Data> _DataCollection = new ObservableCollection<Data>();
public DisplayData()
{
InitializeComponent();
Window window = new Window();
window.Content = new UserControl();
window.Show();
}
public void AddDataToList(int ID, string Name)
{
_DataCollection.Add(new Data
{
StudentID = ID,
StudentName = Name
});
}
public ObservableCollection<Data> DataCollection { get { return _DataCollection; } }
}
public struct Data
{
public int StudentID { get; set; }
public string StudentName { get; set; }
}
下面是如何,我叫它:
class Program
{
[STAThread]
static void Main(string[] args)
{
DisplayData displayData = new DisplayData();
displayData.AddDataToList(2, "John");
System.Threading.Thread.Sleep(5000);
}
}
而XAML:
<UserControl x:Class="ConsoleApplication1.DisplayData"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="210">
<StackPanel>
<ListView ItemsSource="{Binding DataCollection}">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="ID" DisplayMemberBinding="{Binding StudentID}" />
<GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding StudentName}" />
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</UserControl>
编辑 我还想补充一点:
- 它将不会显示我正确的大小
- 我甚至无法看到表格上的列标题
- 没有数据更新。
此外,不知道这是否有帮助,但我已经将此作为一个WPF UserControl添加到我现有的项目。
也许你应该尝试创建一个新的项目(WPF之一),而不是控制台应用程序。 :) – ShadeOfGrey