wpfアプリの画面遷移について

実現したいこと

MainWindow.xamlに配置しているUserControl1.xamlのsettingButtonを押下して表示内容をUserControl2.xamlに切り替えたいです。

発生している問題・エラーメッセージ

エラー BC30456 'DataContext' は 'MainWindowsViewModel' のメンバーではありません。 ColorArrayInspection C:\Users\F000000\source\repos\ColorArrayInspection\ColorArrayInspection\ViewModels\MainWindowsViewModel.vb 48 アクティブ

該当のソースコード

・MainWindowsViewModel.vb
Imports CommunityToolkit.Mvvm.Input
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions

Public Class MainWindowsViewModel
Implements INotifyPropertyChanged

Private _currentView As Object Public Property CurrentView As Object Get Return _currentView End Get Set(value As Object) _currentView = value OnPropertyChanged() End Set End Property Private Sub OnPropertyChanged(<CallerMemberName> Optional propertyName As String = Nothing) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) End Sub Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Private Sub LotText_PreviewTextInput(ByVal sender As Object, ByVal e As TextCompositionEventArgs) ' 半角英数字以外の文字が入力された場合は、イベントをキャンセルする If Not Regex.IsMatch(e.Text, "^[0-9a-zA-Z]+$") Then e.Handled = True End If ' 入力文字数が16文字を超えた場合は、イベントをキャンセルする 'If lotText.Text.Length >= 16 And e.Text <> ChrW(8) Then ' e.Handled = True 'End If End Sub Private _settingButtonCommand As ICommand Public ReadOnly Property SettingButtonCommand As ICommand Get If _settingButtonCommand Is Nothing Then _settingButtonCommand = New RelayCommand(AddressOf SettingButton_Click) End If Return _settingButtonCommand End Get End Property Private Sub SettingButton_Click() Dim mainWindowViewModel As MainWindowsViewModel = TryCast(Me.DataContext, MainWindowsViewModel) mainWindowViewModel.CurrentView = New UserControl2() End Sub

End Class

・UserControl1.xaml <UserControl x:Class="UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ColorArrayInspection" d:DataContext="{d:DesignInstance Type=local:MainWindowsViewModel}" xmlns:vm="clr-namespace:ColorArrayInspection" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.DataContext> <vm:MainWindowsViewModel/> </UserControl.DataContext> <Grid Background="White"> <TextBlock Style="{StaticResource MaterialDesignBody1TextBlock}" x:Name="homeBlock" TextWrapping="Wrap" Background="#7F673AB7" Foreground="White" FontSize="24" VerticalAlignment="Top" Height="42" Text="UserControl1&#xD;&#xA;"/> <TextBox x:Name="lotText" HorizontalAlignment="Left" Height="64" Margin="205,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="310" Style="{DynamicResource MaterialDesignOutlinedTextBox}" MaxLength="16" FontSize="24" VerticalContentAlignment="Center"/> <Label x:Name="messageLabel" Content="メッセージ" Margin="80,185,0,225" FontSize="24" HorizontalAlignment="Left" RenderTransformOrigin="0.472,0.851"/> <TextBlock Style="{StaticResource MaterialDesignHeadline1TextBlock}" x:Name="messageBlock" TextWrapping="Wrap" Margin="80,225,80,50" Background="#19000000" FontSize="48"><Run Language="ja-jp" Text=""/></TextBlock> <Button x:Name="settingButton" Content="設定" Margin="720,5,10,413" Command="{Binding SettingButtonCommand}"/> <Label x:Name="lotLabel" Content="ロット番号" Margin="80,76,0,334" FontSize="24" RenderTransformOrigin="0.472,0.851" VerticalContentAlignment="Stretch" HorizontalAlignment="Left" Width="120"/> </Grid> </UserControl> ### 試したこと ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。

コメントを投稿

0 コメント