-1
안녕하세요 WPF의 ListBox에 사용자 지정 개체 목록을 바인딩하려고합니다.Wpf 바인딩 목록을 사용자 지정 개체로 ListBox
private List<User> users = new List<User>();
public MainWindow()
{
InitializeComponent();
this.users = User.GetAllUsersFromFile();
this.listBox.DataContext = users;
this.listBox.ItemsSource = users;
}
그리고 XML : :
<ListBox x:Name="listBox" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
및 사용자 클래스 : 나는 다음 코드를
private string name;
private byte[] avatar;
public string Name
{
get
{
return this.name;
}
set
{
if (value.Any(c => c == ' '))
throw new Exception("Invalid name. (It cannot contain spaces)");
this.name = value;
}
}
public byte[] Avatar
{
get
{
return this.avatar;
}
set
{
this.avatar = value;
}
}
초기 목록에 새 항목이 있지만 경우, 예상대로 보여주고있다 목록에 추가 (또는 삭제)되면 목록이 업데이트되지 않습니다.
'ObservableCollection users'를 사용하면 문제가 없습니다. –
Sinatr
목록이 동적 또는 정적입니까? –
왜'this.listBox.DataContext = users;'입니까? 그것은 어떤 목적을위한 것이 아닙니다. 문서를 읽는 데 10 분이 걸리면 임의의 값을 임의의 속성에 할당하는 데 2 일 이상 걸릴 수 있습니다. –