此问题已在此处有答案:
Why does WPF support binding to properties of an object, but not fields?(2个答案)
6天前关闭
C# WPF,我试图将类'school'(skola)的字段'name'(naziv)绑定到ComboBox,但它从未显示ComboBox中的学校名称...
这是学校班级:
internal class Skola : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int ID;
private string naziv;
private string adresa;
private string slika;
private ObservableCollection<Ucenik> ucenici;
public Skola() { }
public Skola(int ID, string naziv, string adresa, string slika)
{
this.ID = ID;
this.naziv = naziv;
this.adresa = adresa;
this.slika = slika;
ucenici = new ObservableCollection<Ucenik>();
}
public string Naziv
{
get { return naziv; }
set
{
if (naziv != value)
{
naziv = value;
NotifyPropertyChanged("Naziv");
}
}
}
这是主要的:
public partial class MainWindow : Window
{
private ObservableCollection<Skola> skole;
private Skola NEUPISANI = new Skola(0, "", "", "");
private Skola selected_skolice = new Skola();
public MainWindow()
{
InitializeComponent();
skole = new ObservableCollection<Skola>();
Skola s1 = new Skola(1, "Jozef Atila", "Sarplaninska 8", "");
Skola s2 = new Skola(2, "Bogdana Saputa", "Nardonog fronta 14", "");
s1.dodajUcenikaTest(new Ucenik("0021233213", "Milan", "Reljin", "Radnicka 12", ""));
s2.dodajUcenikaTest(new Ucenik("034321233213", "Dragan", "Markovic", "Crnacka 33", ""));
skole.Add(s1);
skole.Add(s2);
skole.Add(NEUPISANI);
//skolice.DisplayMemberPath = "Naziv";
DataContext = this;
}
这是XAML中的ComboBox:
<ComboBox x:Name="skolice" Margin="180 10 180 30" Grid.ColumnSpan="2"
ItemsSource="{Binding Path=skole}" SelectionChanged="skolice_SelectionChanged"></ComboBox>
如果不是所有的东西都是英语的,这太令人困惑了,我会改的!
2条答案
按热度按时间kadbb4591#
您绑定到的“斯科莱”字段是私有的。将其 Package 在属性中并绑定到该属性:
顺便说一下,你的代码中有一堆不好的做法--这不是你面临的直接问题,但它会降低调试/向人们展示的效率。
mzillmmw2#
由于它现在正在实施,因此存在多种问题。首先,你必须使斯科莱场的公共财产。如果你这样做,组合框将显示保存所有数据的类的类型名称,这样你就可以在列表中看到几个“skola”项。要解决这个问题,你必须在组合框上设置一些属性: