我有一個問題,將一個子類綁定到我的XML文本框中,我遵循這個帖子來做到這一點,但它不工作,沒有使用靜態類。有沒有一種方法可以不使用靜態類?無靜態類綁定文本框
我跟隨這篇文章作爲參考。
Binding textbox values to a model in wpf
我的代碼是:
public class Model:INotifyPropertyChanged{
public event PropertyChangedEventHandler PropertyChanged;
private string title;
public string Title{
get {
return title;
}
set {
if (tilte!= value) {
tilte= value;
NotifyPropertyChanged();
}
}
}
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") {
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class ViewModel{
public Model modelObj;
public ViewModel(){
modelObj= new Model();
this.DataContext = modelObj;
modelObj.Title = "New title"; // <--- this don't update xml
}
}
<Page
x:Class="AppTest.Demo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:m ="using:Models"
xmlns:vm ="using:ViewModels"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.DataContext>
<m:Model></m:Model>
</Page.DataContext>
<Grid>
<TextBlock Text="{Binding Path=Title}"/>
</Grid>
</Page>
你的意思。我已經嘗試並且無法工作。 –
Deimon
作爲魅力!我也想在視圖模型中放入INotifyPropertyChanged接口。謝謝你,工作很好。 – Deimon