城市之星福州网站建设,建行手机,手机开发商,搜外滴滴友链WPF MVVM模式下如何将UI窗口变量传参到Viewmodel层
UI层窗口定义
//窗口中绑定ViewModelhc:GlowWindow.DataContextviewmodel:MainWindowViewModel //hc:GlowWindow.DataContext//注册初始化事件hc:Interaction.Triggershc:EventTrigger…WPF MVVM模式下如何将UI窗口变量传参到Viewmodel层
UI层窗口定义
//窗口中绑定ViewModelhc:GlowWindow.DataContextviewmodel:MainWindowViewModel //hc:GlowWindow.DataContext//注册初始化事件hc:Interaction.Triggershc:EventTrigger EventNameLoadedhc:EventToCommand Command{Binding CmdLoaded} PassEventArgsToCommandTrue//hc:EventTrigger/hc:Interaction.Triggers//需要传递到viewmodel层的UI控件BorderGrid.Column1Margin5Background{DynamicResource MainContentForegroundDrawingBrush}Grid x:NameMainContent/Grid/Border//需要传递到viewmodel层的UI控件 hc:DrawerNameDrawerSourceCodeDockRightMaskCanCloseTrueShowModePushhc:ScrollViewer Width700avalonEdit:TextEditorNameTextContainerMargin5FontFamilyConsolasFontSize10ptIsReadOnlyTrueShowLineNumbersTrueSyntaxHighlightingC# //hc:ScrollViewer/hc:DrawerviewModel层 // 控件private Grid MainContent;private TextEditor TextContainer;/// summary/// 关联控件 通过Lazy关键字 实现延迟初始化/// /summarypublic RelayCommandRoutedEventArgs CmdLoaded new LazyRelayCommandRoutedEventArgs(() new RelayCommandRoutedEventArgs(Loaded)).Value;private void Loaded(RoutedEventArgs e){MainContent (e.Source as MainWindow).MainContent;TextContainer (e.Source as MainWindow).TextContainer;}