아래 코드를 사용하면 Firing C Binding
메시지와 Firing F Binding
메시지가 모두 표시됩니다. 왜 그런가요? Firing C Binding
메시지 만 수신되기를 기대합니다. 내가 NotifyPropertyChanged("F")
앞에있는 주석을 제거하면 I 버튼을 누를 때바운드 속성이 변경되지 않은 경우에도 바인딩이 실행되는 이유는 무엇입니까?
class Program : Form
{
private Label lblC;
private Label lblF;
private Button btnCUp;
private Temp t;
public Program()
{
lblC = new Label();
lblF = new Label();
btnCUp = new Button();
t = new Temp();
lblC.Location = new Point(22, 21);
lblC.Size = new Size(35, 13);
Binding b = new Binding("Text", t, "C");
b.Format += new ConvertEventHandler(CLabelFormat);
lblC.DataBindings.Add(b);
lblF.Location = new Point(108, 21);
lblF.Size = new Size(35, 13);
Binding b2 = new Binding("Text", t, "F");
b2.Format += new ConvertEventHandler(FLabelFormat);
lblF.DataBindings.Add(b2);
btnCUp.Location = new Point(45, 55);
btnCUp.Text = "C Up";
btnCUp.Click += new EventHandler(btnCUp_Click);
this.ClientSize = new Size(165, 113);
this.Controls.Add(lblC);
this.Controls.Add(lblF);
this.Controls.Add(btnCUp);
}
private void CLabelFormat(object sender, ConvertEventArgs cevent)
{
MessageBox.Show("Firing C Binding");
}
private void FLabelFormat(object sender, ConvertEventArgs cevent)
{
MessageBox.Show("Firing F Binding");
}
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());
}
private void btnCUp_Click(object sender, EventArgs e)
{
t.incC();
}
}
class Temp : INotifyPropertyChanged
{
private double tempC;
private double tempF;
public event PropertyChangedEventHandler PropertyChanged;
public Temp()
{
tempC = 0;
tempF = ctof(tempC);
}
private void NotifyPropertyChanged(String field)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(field));
}
}
private double ctof(double c)
{
return c * 9.0/5.0 + 32;
}
public void incC()
{
++tempC;
tempF = ctof(tempC);
NotifyPropertyChanged("C");
//NotifyPropertyChanged("F");
}
public double C
{
get { return tempC; }
}
public double F
{
get { return tempF; }
}
}
, 그럼 나는 네 개의 메시지 상자를 얻을. 2 개는 "Firing C Binding"용이고 2 개는 "Firing F Binding"용입니다.
각 코드 중 하나만 가져 오도록 코드를 수정할 수 있습니까?
편집 : 나는 바인딩하여 PropertyChanged 이벤트가 발생할 때 그것이 무엇을보고 (리플렉터를 사용)에 대한 소스를보고했지만, 아무것도 찾을 수 couln't 한. 누구나 통찰력을 제공 할 수 있습니까? 나는 그것이 어떤 분야가 바뀌 었는지에 대해 관심이 있는지 확인하기를 원합니다.
편집 : 내 코드를 문제를 설명하는 전체 컴파일 가능한 예제 구현으로 바 꾸었습니다.
"한 속성이 변경되면 다른 속성에 영향을 미쳤음을 나타냅니다." 어디에서 "표시"합니까? NotifyPropertyChanged 구현을 원래 게시물에 추가하여 거기에 무슨 일이 일어나고 있는지 보여줍니다. – Tobbe
PropertyChanged 이벤트에 관하여 : 듣고있는 Binding 클래스가 아닌가? 나는 MS가 그 클래스를 구현할 때 속성 이름을 무시하기로 결정하지 않았기를 바란다 :) – Tobbe
나는 또한 그렇게 희망하지만, 이상한 일이 일어났다. –