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