要素のリストをリストビューにバインドしようとしていますが、各要素には特性がありますが、次のプロパティのみをバインドする必要があります。要素の単一のプロパティをリストビュー(UWP)にバインドする
これは非常にはっきりしていますが、私にとってはうまくいかず、リストビューは空のままです...リストビューにバインドするリストに要素が含まれていて、それらが含まれているかどうかを確認しました。ソースどこで
public class MainType
{
public string Type { get; set;}
}
public class ChildType : MainType
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
public class SimpleClass
{
public ObservableCollection<ChildType> SimpleList { get; set; }
public SimpleClass()
{
SimpleList = new ObservableCollection<ChildType>();
SimpleList.Add(new ChildType { Prop1 = "prop1", Prop2 = "prop2", Type = "type1" }
SimpleList.Add(new ChildType { Prop1 = "prop1", Prop2 = "prop2", Type = "type2" }
}
}
:
はまた、私は、単純な「リストMYLIST」とListView.ItemTemplateなしをバインドしようとしました、それは私のコードの例に続きに動作します、私はこの構造を持っています
:C#の側に <ListView x:Name="myListView">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Type}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
と、この:私は、私は、XAML側でこれを私のリストビューを持っています3210
public sealed partial class MyUserControl : UserControl
{
private SimpleClass MySimpleClass;
public MyUserControl()
{
this.InitializeComponent();
myListView.DataContext = MySimpleClass = new SimpleClass();
myListView.ItemsSource = MySimpleClass.SimpleList;
}
}
誰かが私に何が欠けているか教えていただけますか?
ありがとうございます!
[編集:上記のコードは正しく、問題は別のものでした。 ]
別の方法でSimpleClass内に問題がありました.JsonファイルをSimpleListにデシリアライズして、バインドに問題が発生しました。後で単純にADDメソッドで要素を追加するとバインディングが機能しませんでした... jsonファイルを別のリストにデシリアライズする場合:TempListとその後foreachを使用して、TempListのすべての要素をSimpleListに追加します。
バインディングが動作しません:
SimpleList = JsonConvert.DeserializeObject<ObservableCollection<ChildType>>(text, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
バインディング作品:
TempList = JsonConvert.DeserializeObject<ObservableCollection<ChildType>>(text, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
foreach(ChildType item in TempList)
{
SimpleList.Add(item);
}
私は以下のコメントを読んでいます。あなたはUIにリストに追加された項目を反映するためにSimpleClassでINotifyPropertyChangedを実装する必要があります – Archana
こんにちはLovetoCode、あなたの返信ありがとう、INotifyPropertyChangedは、プロパティが変更されたときにリストを更新するために使用されていますが、これに対して、私はINotifyPropertyChangedを使用していませんが、ObservableCollection <>を単純なリスト<>で使用しました。あなたのヒントを試してみます。ありがとうございます! SimpleList = JsonConvert.DeserializeObject <のObservableCollection>(テキスト、新しいJsonSerializerSettings {TypeNameHandling = TypeNameHandling.Auto:私は、JSONファイルからSimpleListに新しい要素を追加し、私のコードではありません –
Dendi
INotifyPropertyChangedの助けが、私は問題を発見しません.. }); これはうまくいきません。また、コードトラフのADDメソッドから別の要素を追加すると、この要素もリストビューに表示されません。 私は別のリストを作成する場合:TempList、私は私のjsonファイルを逆シリアル化し、その後、トラフをTempListからforeach私はSimpleListにすべての要素を追加、それは動作します! :) – Dendi