2014-11-24 3 views
0

내 win 8 app에 간단한 create-function을 구현하려고 시도합니다. 임 MVVM 패턴을 사용하려고합니다. 내 view.model을 사용하여 클래스를 내 뷰에 전달하려고 시도한 다음 새 객체를 만들 수있는 몇 개의 텍스트 상자를 간단히 가져와야합니다. MVC 애플리케이션에서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; } 
} 

내가 어떤 @Html.TextBoxFor을 수행하고 이후 방법을 만든 것이다 다음은 뷰 모델 및 클래스입니다. XAML에서이 작업을 수행하는 방법을 잘 모릅니다. viewmodel이 전달되도록 전달됩니다. 나는이 같은 속성을 ACESS 수 있습니다

<TextBox Grid.Row="0" Text="{Binding Path=Place.Title}"/> 
<TextBox Grid.Row="0" Text="{Binding Path=Place.Description}"/> 

하지만 난 다시 뷰 모델에 어떻게 할 수있는 "후"새로운 가치를 이해하고 새로운 개체를 만들 수 있습니까?

편집 :

나는이 내 뷰 모델의 명령이하는 방법입니다 무엇을 볼 수에서

:

<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> 

오전 : 나는 또한 내 XAML이 코드를 추가

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? 
       }); 
     } 
    } 

을 나는 여기서 옳은 길을 가고 있니? 꽤 혼란 스럽네요 =)

+1

을 뱉어 것입니다. 값은 기본 속성에 대해 두 번 또는 두 번 데이터 바인딩됩니다. "CreatePlace"명령에 첨부 된 단추를 추가 할 수 있습니다. –

+0

감사합니다. 승인. "CreatePlace"는 내 ViewModel의 메소드입니까? – Wranglerino

+0

아니요, MVVM을 따르는 것이 바람직합니다. –

답변

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; } 
} 

처럼 보일 수 있고 명령 구현 저장 단순한이 당신에게 UI를 줄 것이다,

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

,

그리고 버튼을 클릭하면 다음 당신은 아무것도 게시하지 않습니다 MVVM/XAML에서

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

대단히 고마워요! 나는 이것을 공부할 것이다! – Wranglerino