我試圖在我的win 8應用中實現一個簡單的創建功能。我試圖使用MVVM模式。我試圖通過我的view.model傳遞一個類到我的視圖,然後只是有幾個文本框,讓我創建一個新的對象。這裏是視圖模型和類:在win 8 store app中創建新對象
public class CreateViewModel : ViewModelBase
{
public Place Place { get; set; }
}
public class Place
{
[PrimaryKey, AutoIncrement]
public int PlaceId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
在一個MVC應用我會做一些@Html.TextBoxFor
,創造了後方法。 在XAML中,我不確定如何執行此操作。 viewmodel被傳入以查看它應該顯示的內容。我可以接取它的屬性是這樣的:
<TextBox Grid.Row="0" Text="{Binding Path=Place.Title}"/>
<TextBox Grid.Row="0" Text="{Binding Path=Place.Description}"/>
但我不明白,怎麼可以「後」新值回視圖模型,並創建一個新的對象?
編輯:
從我可以看到這是一個辦法讓我的視圖模型的命令:
public class CreateViewModel : ViewModelBase
{
public RelayCommand CreatePlaceCommand
{
get;
private set;
}
public Place Place { get; set; }
public CreateViewModel()
{
InitializeCommands();
}
private void InitializeCommands()
{
CreatePlaceCommand =
new RelayCommand(() =>
{
//What goes here?
});
}
}
我還添加了此代碼,以我的XAML:
<TextBox Grid.Row="0" Text="{Binding Place.Title,Mode=TwoWay}"/>
<TextBox Grid.Row="0" Text="{Binding Place.Description,Mode=TwoWay}"/>
<Button Grid.Row="0" Content="Click"
Command="{Binding CreatePlaceCommand}" >
</Button>
上午我在正確的軌道上?它很迷惑=)
在MVVM/XAML你沒有張貼任何。值只是一個/雙向數據綁定到基礎屬性。也許你可以添加附加到「CreatePlace」命令的按鈕。 – 2014-11-24 13:02:07
謝謝!好。 「CreatePlace」是我的ViewModel上的一種方法? – Wranglerino 2014-11-24 13:10:01
不,最好是遵循MVVM的命令。 – 2014-11-24 13:14:40