1
我有一個屬性的簡單類,這個類實現了接口INotifyPropertyChange。WPF中的單向綁定的簡單問題
public class SomeClass : INotifyPropertyChanged
{
private string _iD;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string ID
{
get { return _iD; }
set
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("ID can not be null or empty");
if (this.ID != value)
{
_iD = value;
NotifyPropertyChanged(ID);
}
}
}
}
我試着把OneWay綁定到標籤上。我在代碼隱藏中設置了標籤dataContext。
private SomeClass _myObject;
public MainWindow()
{
InitializeComponent();
_myObject = new SomeClass() { ID = "SomeID" };
lb.DataContext = _myObject;
}
在XAML中我綁定屬性ID從標籤的內容。
<Label Name="lb" Content="{Binding Path = ID, Mode=OneWay}" Grid.Row="0"></Label>
<TextBox Name="tb" Grid.Row="1"></TextBox>
<Button Name="btn" Content="Change" Height="20" Width="100" Grid.Row="2" Click="btn_Click"></Button>
然後我更改按鈕單擊事件中的屬性ID的值,但標籤的內容沒有改變。
private void btn_Click(object sender, RoutedEventArgs e)
{
_myObject.ID = tb.Text;
Title = _myObject.ID;
}
哪裏有問題?
謝謝你的提前 – Tom 2010-10-31 12:26:53