[英]Binding dynamic objects wpf
我試圖綁定一個可以動態更改為顯示元素的自定義對象。
我的window.xaml現在有這個:
<StackPanel Height="310" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Configuration_stackPanel" VerticalAlignment="Top" Width="264" Grid.Column="1">
<Label Content="{Binding Path=Client}" Height="22" HorizontalAlignment="Left" Margin="20,0,0,0" Name="Client" VerticalAlignment="Top" />
</StackPanel>
在window.xaml.cs中,我有成員
public CustomObject B;
CustomObject有一個客戶端成員。 B.Client,獲取客戶端名稱(這是一個字符串)等
我該怎么做才能顯示B.Client並在代碼更改時更改它。
即:如果在代碼中我做B.Client =“foo”則顯示foo,如果我做B.Client =“bar”,則顯示bar而不是foo。
提前致謝
F
您的CustomObject
類必須實現INotifyPropertyChanged
接口:
public class CustomObject : INotifyPropertyChanged
{
private string _client;
public string Client
{
get { return _client; }
set
{
_client = value;
OnPropertyChanged("Client");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.