2017-12-21 7 views
0

これは、2つのビュー間の切り替えを説明するうえで、さらに多くのことを行うこのWalkthroughをフォローしています。2つのビューを並行してバインドする方法

2つのビューを切り替えるのではなく、2つのビューを並べて表示します。 ()ビュー

マイコード

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でそれらを必要とせず、私はビューを切り替えることはありません、彼らは同じ時間に並んでいる。だから、私は何をする必要があるか考えていた私が直面してる問題は私のグリッドに、これらのViewModelビューをバインドする方法

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

リンクした例は、表示するビューを制御するために「NavigateMessage」の使用を容易にするためにViewVMを使用しているようです。ビューを切り替えていないので、まだViewVMが必要な理由はありますか?作者がMVVMのアプローチだと言っても、viewmodelクラスのusercontrolを格納するのはMVVMではありません。私はMVVMの純粋主義者ではありません、私はちょうどそのメカニズムを使用するように問題を解決する複雑な感じです。また、あなたのAuthorizedMobileDevicesVM.Viewが本当に何らかの種類のユーザコントロールであれば、それは動作しなければならない(SHOULD)。バインディングエラーがないことを確認しましたか? – 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

関連する問題