2014-11-24 16 views
0

我試圖在我的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> 

上午我在正確的軌道上?它很迷惑=)

+1

在MVVM/XAML你沒有張貼任何。值只是一個/雙向數據綁定到基礎屬性。也許你可以添加附加到「CreatePlace」命令的按鈕。 – 2014-11-24 13:02:07

+0

謝謝!好。 「CreatePlace」是我的ViewModel上的一種方法? – Wranglerino 2014-11-24 13:10:01

+0

不,最好是遵循MVVM的命令。 – 2014-11-24 13:14:40

回答

2

在這裏,研究這個簡單的例子來獲得MVVM/DataBinding/Commands。這真的很簡單,但它應該顯示使用的「模式」。有很多庫(比如MVVMLight)可以讓指令更簡單,更強大。

因此,假如我們有Place實體

public class Place 
{ 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 
    public override string ToString() 
    { 
     return string.Format("Id={0},Title={1},Description={2}", 
      Id, Title, Description); 
    } 
} 

,你們便在名爲wpfApplication1

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:wpfApplication1="clr-namespace:WpfApplication1" 
     Title="MainWindow" 
     Height="116" 
     Width="250"> 

    <!-- set datacontext to mainviewmodel --> 
    <Window.DataContext> 
     <wpfApplication1:MainViewModel /> 
    </Window.DataContext> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="30" /> 
     </Grid.RowDefinitions> 

     <!-- input textboxes for title and description --> 
     <StackPanel Grid.Row="0"> 
      <TextBox Text="{Binding Place.Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="25" /> 
      <TextBox Text="{Binding Place.Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="25" /> 
     </StackPanel> 

     <!-- button bound to save command, declared in viewmodel --> 
     <Button Grid.Row="1" Content="Save" Command="{Binding SaveCommand}" /> 
    </Grid> 
</Window> 

相關MainWindows.xaml.cs應用有MainWindow.xaml只含有InitializeComponents()

現在你MainViewModel,「採取一切後事」,可能看起來像

public class MainViewModel 
{ 
    private Place _place; 

    public MainViewModel() 
    { 
     // create and register new save command 
     SaveCommand = new SaveCommand(this); 
     CommandManager.RegisterClassCommandBinding(
      typeof(MainViewModel), new CommandBinding(SaveCommand)); 
    } 

    // property to hold place data, exposed in UI 
    public Place Place 
    { 
     get { return _place ?? (_place = new Place()); } 
     set { _place = value; } 
    } 

    public ICommand SaveCommand { get; private set; } 
} 

而且在視圖模型簡單的保存命令執行使用現在

public class SaveCommand : ICommand 
{ 
    public event EventHandler CanExecuteChanged; 
    private readonly MainViewModel _context; 

    public SaveCommand(MainViewModel context) 
    { 
     _context = context; 
    } 

    public void Execute(object parameter) 
    { 
     Console.WriteLine(string.Format("Do something with {0}", _context.Place)); 
    } 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 
} 

,這將給你的用戶界面,東西像下面一樣(這個例子不是Store類型)

enter image description here

然後點擊一個按鈕就吐了出來

Do something with Id=0,Title=Title,Description=and teh description 
+0

非常感謝!我會研究這個! – Wranglerino 2014-11-24 13:57:10