我是初學者在mvvm concept.so我試過一個示例application.it包含兩個文本框是名稱和id,一個提交按鈕,一個label.when我點擊提交按鈕時,它結合了兩個字符串從文本框中顯示並顯示在標籤中。在mvvm視圖不更新
我可以提交值,並在viewmodel屬性包含result.but它不顯示在view.why ..?
鑑於包含
<grid>
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding name}" Height="23" Margin="9" Name="txtname" Width="120" />
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding id}" Height="23" Margin="9" Name="txtid" Width="120" />
<Button Command="{Binding submit}" Content="submit" Grid.Column="2" Grid.Row="2" Height="23" Name="btnsubmit" Width="75" />
<Label Content="{Binding display}" Grid.Row="3" Height="38" HorizontalAlignment="Left" Name="lbldisplay" Width="192" />
</grid>
view.cs代碼
public MainWindow()
{
InitializeComponent();
DataContext = new DemoViewModel();
}
在我的視圖模型包含兩個.cs文件
一個是DemoViewModelInotify.cs.in這個我寫的代碼for inotifypropertychanged。 另一個是DemoViewModel.cs.這個包含屬性和命令。
namespace mvvmdemonixon.ViewModel
{
public class DemoViewModel :DemoViewModelInotify
{
public ICommand submit { get; set; }
public string name { get; set; }
public string display { get; set; }
public int id{get;set;}
public DemoModel model { get; set; }
public DemoViewModel()
{
submit = new DelegateCommand<object>(this.add);
}
public void add(object paramter)
{
string a= mvvmdemonixon.Model.DemoModel.addstring(name, id);
display = a;
}
}
}
我的模型包含
namespace mvvmdemonixon.Model
{
public class DemoModel
{
public static string addstring(string name1, int no1)
{
string display = "The Student Name Is " + name1 + "and Id Is" + no1 + ".";
return display;
}
}
}
我app.xaml.cs
private void OnStartup(object sender, StartupEventArgs e)
{
mvvmdemonixon.MainWindow view = new MainWindow();
view.DataContext = new mvvmdemonixon.ViewModel.DemoViewModel();
view.Show();
}
在我的應用程序的XAML
<Application x:Class="mvvmdemonixon.App"
Startup="OnStartup">
</Application>
提前感謝..
嗨@ michael我添加了inotifypropertychanged並使屬性像above.it打onpropertychanged但它沒有更新視圖..爲什麼..? – nichu09
@ nixen09:請確保您使用雙向綁定 - '{Binding Name,Mode = TwoWay}' – MichaelS