私はこれを一日中試してきましたが、運がありませんでした。UserControlを表示することはできますが、XAMLで列を設定しても、列が表示されない理由を理解できません。また、フォームが空白で開いているため、データが表示されているかどうかもわかりません。
これは、別のプログラムから呼び出しているクラスライブラリの一部です。ただし、常に空白のフォームが表示され、何も更新されていません。
以下は私の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>
編集私もそれを追加したいと思います:
- 正しいサイズが表示されません
- フォームの列見出しも表示されません
- データの更新もありません。
また、これが役立つかどうかはわかりませんが、これをWPFUserControlとして既存のプロジェクトに追加しました。