2017-12-21 3 views
0

다음은이 두 가지 뷰 간의 전환을 설명하는 훌륭한 작업을 수행하는 Walkthrough을 따르고 있습니다.두 개의 뷰를 나란히 바인딩하는 방법

두 가지보기를 전환하는 대신 두 가지보기를 나란히 표시합니다.() 뷰

내 코드

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     // Apply default form level font style 
     Style = (Style)FindResource(typeof(Window)); 
     Messenger.Default.Register<NavigateMessage>(this, (action) => ShowUserControl(action)); 
     this.DataContext = new MainWindowViewModel(); 
    } 

    private void ShowUserControl(NavigateMessage nm) 
    { 
     EditFrame.Content = nm.View; 
    } 
} 
을 설정 탐색 ShowUserControl를 호출 MainWindow.xaml.cs를에서

public class MainWindowViewModel : NotifyUIBase 
{ 
    public ObservableCollection<ViewVM> Views {get;set;} 

    public MainWindowViewModel() 
    { 
     ObservableCollection<ViewVM> views = new ObservableCollection<ViewVM> 
     { 
      new ViewVM{ ViewDisplay="Customers", ViewType = typeof(CustomersView), ViewModelType = typeof(CustomersViewModel)}, 
      new ViewVM{ ViewDisplay="Products", ViewType = typeof(ProductsView), ViewModelType = typeof(ProductsViewModel)} 
     }; 
     Views = views; 
     RaisePropertyChanged("Views"); 
     views[0].NavigateExecute(); 
    } 
} 

:

앤디는 OC는 그의 MainWindowViewModel 배치 ViewModels에 다음과 같은 설정 :

나는 OC에서 그들을 필요로하지 않을 것이다. 그리고 나는보기를 바꾸지 않을 것이다. 그들은보기에 표시 될 것이다. 같은 시간에 나란히. 그래서 내가해야 할 일을 생각하면 내가 직면하고있어 문제는 내 그리드에서 이러한 뷰 모델 뷰를 바인딩하는 방법이다

public class MainWindowViewModel : NotifyUIBase 
{ 
    private ViewVM m_MobileDeviceRequestsVM; 
    private ViewVM m_AuthorizedMobileDevicesVM; 

    public ViewVM MobileDeviceRequestsVM 
    { 
     get { return m_MobileDeviceRequestsVM; } 
    } 

    public ViewVM AuthorizedMobileDevicesVM 
    { 
     get { return m_AuthorizedMobileDevicesVM; } 
    } 

    public MainWindowViewModel() 
    { 
     m_MobileDeviceRequestsVM = new ViewVM { ViewDisplay = "MobileDeviceRequests", ViewType = typeof(MobileDeviceRequestsView), ViewModelType = typeof(MobileDeviceRequestsViewModel) }; 
     m_AuthorizedMobileDevicesVM = new ViewVM { ViewDisplay = "AuthorizedMobileDevices", ViewType = typeof(AuthorizedMobileDevicesView), ViewModelType = typeof(AuthorizedMobileDevicesViewModel) }; 
    } 
} 

, 즉 작동하지 않습니다하지만 ContentControl을 몇 가지를 사용했습니다. 어떻게해야합니까?

<Window x:Class="MobileDeviceAuthenticator.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:MobileDeviceAuthenticator" 
     Title="Device Authorization" Height="381" Width="879"> 
    <Grid> 
     <Grid Margin="0,25,0,0"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 

      <Label Content="Authorized Devices" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" Margin="20,10,0,0" VerticalAlignment="Top" /> 
      <ContentControl Grid.Row="1" Grid.Column="0" Content="{Binding AuthorizedMobileDevicesVM.View}" /> 

      <Label Content="Device Requests" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Margin="20,10,0,0" VerticalAlignment="Top" /> 
      <ContentControl Grid.Row="1" Grid.Column="1" Content="{Binding MobileDeviceRequestsVM.View}" /> 
     </Grid> 
    </Grid> 
</Window> 
+1

링크 된 예제는 ViewVM을 사용하여 "NavigateMessage"를 사용하여 어떤 뷰를 표시할지 제어하는 ​​것처럼 보입니다. 뷰를 전환하지 않으므로 여전히 ViewVM이 필요한 이유가 있습니까? 제작자가 MVVM 접근 방식이라고해도 viewmodel 클래스로 usercontrol을 저장하는 것은 MVVM이 아닙니다. MVVM 순수 주의자가 아니더라도, 그 메커니즘을 사용하면 문제에 대한 해결책이 복잡해집니다. 또한 AuthorizedMobileDevicesVM.View가 실제로 어떤 종류의 usercontrol 일 경우 - 작동해야합니다. 바인딩 오류가 없음을 확인 했습니까? – Rowbear

+0

님께 도움이 될 것 같아서 : https://www.youtube.com/watch?v=xUwk2-_tRzo – springathing

답변

1

접근 방식에 대한 내 예약과 관련하여 의견을 제시 한 후 예제의 ViewVM 클래스를 다시 살펴 보았습니다. 그 중 하나를 무시하고 아래 예제의 ViewVM 코드를 수정하지 않은 가정 :

public class ViewVM 
    { 
     public string ViewDisplay { get; set; } 
     public Type ViewType { get; set; } 
     public Type ViewModelType { get; set; } 
     public UserControl View { get; set; } 
     public RelayCommand Navigate { get; set; } 
     public ViewVM() 
     { 
      Navigate = new RelayCommand(NavigateExecute); 
     } 
     public void NavigateExecute() 
     { 
      if(View == null && ViewType != null) 
      { 
       View = (UserControl)Activator.CreateInstance(ViewType); 
      } 
      var msg = new NavigateMessage { View = View, ViewModelType = ViewModelType, ViewType = ViewType }; 
      Messenger.Default.Send<NavigateMessage>(msg); 
     } 
    } 

문제 것은 NavigateExecute가 호출 될 때 View 속성에만 반사를 통해 할당된다는 점이다. AuthorizedMobileDevicesVM.View에 바인딩하면 아직 인스턴스화되지 않았습니다. 리플렉션 코드를 케이스 생성자로 옮기면 작동합니다. 물론 이것은 페이지 네비게이션을 위해 다른 곳에서 ViewVM을 사용한다면 애플리케이션의 메모리 사용량을 증가시킬 것임을 의미합니다. 이는 필요에 따라보기 만 생성하려는 디자인과 같습니다.

+0

예약 내용을 알려 주셔서 감사합니다. 나는이 경우에 필요하지 않다는 데 동의 함으로이 특정 창을 위해 ViewVM을 사용하지 않기로했습니다. 당신은 또한 내가 아직 알지 못했던 이유와 해결책을주었습니다. 감사합니다! – Hank

관련 문제