WPF中的MVVM模式 [英] MVVM pattern in WPF
问题描述
我在MainWindow.xaml,MainWindow.xaml.cs中具有以下应用程序,
I Have the following application with MainWindow.xaml , MainWindow.xaml.cs,
GridViewModel.cs和GridModel.cs
GridViewModel.cs and GridModel.cs
我想知道这个应用程序是否具有正确的MVVM格式,它是从WCF服务获取数据的,
I want to know is this app is in proper MVVM format , it gets its data from a WCF service,
,在视图中启动
在视图中,模型是Getdata和Updata方法
in the Model service is initiated and in View Model is Getdata and Updata methods in View
数据绑定到GridControl,数据值在MainWindow.xaml.cs文件中更新
Data is bound to GridControl and data values are updated in the MainWindow.xaml.cs file
我不使用Inotify属性更改的界面,
I donot use Inotifypropertychanged interface ,
我像在View Model中启动模型一样
I initiate the Model in View Model like
GridModel gm =新的GridMode();
GridModel gm=new GridMode();
然后在
GridViewModel Gym = new GridViewModel();
GridViewModel gym=new GridViewModel();
然后访问那里的方法,在适当的MVVM中也是如此,如果存在缺点,那么我需要进行哪些更改.
then access there methods , so is this in proper MVVM and if there ae shortcomings then what changes do I need to make.
MainWindow.xaml
MainWindow.xaml
< Window
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg =" http://schemas.devexpress.com/winfx/2008/xaml/grid". xmlns:dx =" http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxe =" http://schemas.devexpress.com/winfx/2008/xaml/editors". xmlns:dxn =" http://schemas.devexpress.com/winfx/2008/xaml/navbar" x:Class ="UsingWCFServiceInWPFApp.MainWindow"
xmlns:local ="clr-namespace:UsingWCFServiceInWPFApp"标题="MainWindow".高度="482".宽度="1150">
<!-< Window.DataContext>
< local:GridModel/>
</Window.DataContext> ItemsSource ="{Binding emp}"->
< Grid>
< DockPanel>
< dxg:GridControl x:Name =" GcView" DesignTimeShowSampleData ="False". AutoGenerateColumns ="AddNew"
nbsp; bsp EnableSmartColumnsGeneration ="True"宽度="828". >
< dxg:GridControl.Resources>
< SolidColorBrush x:Key =" UnfocusedRowBrush"颜色=#FFDDDDDD" />
< ControlTemplate xmlns:dxi ="http://schemas.devexpress.com/winfx/2008/xaml/core/internal"
xmlns:dxgt =" http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
x:Key =" {dxgt:GridRowThemeKey IsThemeIndependent = True,ResourceKey = RowTemplate}"
TargetType =" {x:Type dxg:RowControl}>
b < Grid>
nbsp; bsp <边界x:Name =背景"; Background ="{TemplateBinding Background}" />
nbsp; bsp < Border x:Name ="BottomLine" BorderThickness ="0,0,0,1". BorderBrush =" {TemplateBinding
BorderBrush}" VerticalAlignment =底部". />
nbsp; bsp < Grid x:Name =" PART_LayoutPanel" />
nbsp; bsp < dx:SimpleButton x:Name =" PART_EditButton"内容=编辑"字形="{dx:DXImage
Image = Edit_16x16.png}"
Command =" {Binding View.Commands.ShowEditForm}"可见性=收合" HorizontalAlignment =右".保证金="4".填充="30,4" />
nbsp; bsp </Grid>
b < ControlTemplate.Triggers>
nbsp; bsp < MultiDataTrigger>
nbsp; bsp < MultiDataTrigger.Conditions>
nbsp; bsp <条件绑定=" {Binding IsMouseOver,
RelativeSource = {RelativeSource Self}}'";值=真". />
nbsp; bsp <条件绑定=" {Binding IsEditFormVisible}"
值=假". />
nbsp; bsp </MultiDataTrigger.Conditions>
nbsp; bsp < Setter TargetName =" PART_EditButton"属性=可见性".
值=可见". />
nbsp; bsp </MultiDataTrigger>
nbsp; bsp < Trigger Property =" ShowHorizontalLine"值="False">
nbsp; bsp < Setter Property ="Visibility"值=已崩溃"
TargetName ="BottomLine" />
nbsp; bsp </Trigger>
nbsp; bsp < Trigger Property =" ShowBottomLine"值="True">
nbsp; bsp < Setter Property ="Visibility"值=可见". TargetName ="BottomLine"
/>
nbsp; bsp </Trigger>
nbsp; bsp < Trigger Property ="FadeSelection";值="True">
nbsp; bsp < Setter Property ="Background"值=" {StaticResource
UnfocusedRowBrush}"; />
nbsp; bsp </Trigger>
nbsp; bsp < Trigger Property =" ShowRowBreak"值="True">
nbsp; bsp < Setter Property ="BorderThickness"值="{dxi:ThemeResource
{dxgt:GridRowThemeKey ResourceKey = RowBreakThickness}}" TargetName ="BottomLine" />
nbsp; bsp < Setter Property ="BorderBrush"值="{dxi:ThemeResource
{dxgt:GridRowThemeKey ResourceKey = RowBreakBrush}}" TargetName ="BottomLine" />
nbsp; bsp </Trigger>
nbsp; bsp < Trigger Property ="FixedRowPosition"值=底部">
nbsp; bsp < Setter Property ="VerticalAlignment";值=最高".
TargetName ="BottomLine" />
nbsp; bsp </Trigger>
nbsp; bsp < Trigger Property ="IsLastFixedRow";值="True">
nbsp; bsp < Setter Property ="BorderThickness"值="0,1,0,1".
TargetName ="BottomLine" />
nbsp; bsp < Setter Property ="Background"值="{dxi:ThemeResource
{dxgt:GridRowThemeKey ResourceKey = RowSeparatorBrush}}" TargetName ="BottomLine" />
nbsp; bsp </Trigger>
b </ControlTemplate.Triggers>
</ControlTemplate>
</dxg:GridControl.Resources>
< dxg:GridControl.View>
< dxg:TableView EditFormShowMode ="InlineHideRow"; ShowEditFormOnDoubleClick =" False" ShowEditFormOnEnterKey =" False"
ShowEditFormOnF2Key =" False" RowUpdated ="TableView_RowUpdated"; />
</dxg:GridControl.View>
< dxg:GridColumn x:Name ="EmpID"; FieldName ="EmpID"; ReadOnly ="True" EditFormVisible ="False"/>
< dxg:GridColumn x:Name ="名称" FieldName =名称"/>
< dxg:GridColumn x:Name ="电子邮件" FieldName =电子邮件"/>
< dxg:GridColumn x:Name =性别"; FieldName =性别"/>
< dxg:GridColumn x:Name ="电话" FieldName =电话"/>
</dxg:GridControl>
</DockPanel>
< DockPanel>
< dx:SimpleButton x:Name =" Showdata" Content =显示数据" HorizontalAlignment =左"高度="41".保证金="6,172,0,0&". VerticalAlignment =顶部"
宽度="115". Click =" SimpleButton_Click"/>
</DockPanel>
</Grid>
</Window>
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dxn="http://schemas.devexpress.com/winfx/2008/xaml/navbar" x:Class="UsingWCFServiceInWPFApp.MainWindow"
xmlns:local="clr-namespace:UsingWCFServiceInWPFApp" Title="MainWindow" Height="482" Width="1150">
<!--<Window.DataContext>
<local:GridModel/>
</Window.DataContext> ItemsSource="{Binding emp}"-->
<Grid>
<DockPanel>
<dxg:GridControl x:Name="GcView" DesignTimeShowSampleData="False" AutoGenerateColumns="AddNew"
EnableSmartColumnsGeneration="True" Width="828" >
<dxg:GridControl.Resources>
<SolidColorBrush x:Key="UnfocusedRowBrush" Color="#FFDDDDDD" />
<ControlTemplate xmlns:dxi="http://schemas.devexpress.com/winfx/2008/xaml/core/internal"
xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
x:Key="{dxgt:GridRowThemeKey IsThemeIndependent=True, ResourceKey=RowTemplate}"
TargetType="{x:Type dxg:RowControl}">
<Grid>
<Border x:Name="Background" Background="{TemplateBinding Background}" />
<Border x:Name="BottomLine" BorderThickness="0,0,0,1" BorderBrush="{TemplateBinding
BorderBrush}" VerticalAlignment="Bottom" />
<Grid x:Name="PART_LayoutPanel" />
<dx:SimpleButton x:Name="PART_EditButton" Content="Edit" Glyph="{dx:DXImage
Image=Edit_16x16.png}"
Command="{Binding View.Commands.ShowEditForm}" Visibility="Collapsed" HorizontalAlignment="Right" Margin="4" Padding="30,4" />
</Grid>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsMouseOver,
RelativeSource={RelativeSource Self}}" Value="True" />
<Condition Binding="{Binding IsEditFormVisible}"
Value="False" />
</MultiDataTrigger.Conditions>
<Setter TargetName="PART_EditButton" Property="Visibility"
Value="Visible" />
</MultiDataTrigger>
<Trigger Property="ShowHorizontalLine" Value="False">
<Setter Property="Visibility" Value="Collapsed"
TargetName="BottomLine" />
</Trigger>
<Trigger Property="ShowBottomLine" Value="True">
<Setter Property="Visibility" Value="Visible" TargetName="BottomLine"
/>
</Trigger>
<Trigger Property="FadeSelection" Value="True">
<Setter Property="Background" Value="{StaticResource
UnfocusedRowBrush}" />
</Trigger>
<Trigger Property="ShowRowBreak" Value="True">
<Setter Property="BorderThickness" Value="{dxi:ThemeResource
{dxgt:GridRowThemeKey ResourceKey=RowBreakThickness}}" TargetName="BottomLine" />
<Setter Property="BorderBrush" Value="{dxi:ThemeResource
{dxgt:GridRowThemeKey ResourceKey=RowBreakBrush}}" TargetName="BottomLine" />
</Trigger>
<Trigger Property="FixedRowPosition" Value="Bottom">
<Setter Property="VerticalAlignment" Value="Top"
TargetName="BottomLine" />
</Trigger>
<Trigger Property="IsLastFixedRow" Value="True">
<Setter Property="BorderThickness" Value="0,1,0,1"
TargetName="BottomLine" />
<Setter Property="Background" Value="{dxi:ThemeResource
{dxgt:GridRowThemeKey ResourceKey=RowSeparatorBrush}}" TargetName="BottomLine" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</dxg:GridControl.Resources>
<dxg:GridControl.View>
<dxg:TableView EditFormShowMode="InlineHideRow" ShowEditFormOnDoubleClick="False" ShowEditFormOnEnterKey="False"
ShowEditFormOnF2Key="False" RowUpdated="TableView_RowUpdated" />
</dxg:GridControl.View>
<dxg:GridColumn x:Name="EmpID" FieldName="EmpID" ReadOnly="True" EditFormVisible="False"/>
<dxg:GridColumn x:Name="Name" FieldName="Name"/>
<dxg:GridColumn x:Name="Email" FieldName="Email"/>
<dxg:GridColumn x:Name="Gender" FieldName="Gender"/>
<dxg:GridColumn x:Name="Phone" FieldName="Phone"/>
</dxg:GridControl>
</DockPanel>
<DockPanel>
<dx:SimpleButton x:Name="Showdata" Content="Show Data" HorizontalAlignment="Left" Height="41" Margin="6,172,0,0" VerticalAlignment="Top"
Width="115" Click="SimpleButton_Click"/>
</DockPanel>
</Grid>
</Window>
推荐答案
在我的选择中,您的模式是UI-业务逻辑数据访问层.不是MVVM,您缺少ViewModel.该视图绑定到ViewModel的属性,而ViewModel的属性又公开了模型对象中包含的数据以及该视图特定的其他状态.绑定 view和ViewModel之间的构造很容易构造,因为ViewModel对象被设置为视图的DataContext.如果ViewModel中的属性值发生更改,这些新值将通过数据绑定自动传播到视图.用户单击按钮时 在视图中,将执行ViewModel上的命令以执行请求的操作.
In my option, your pattern is UI-Business Logic-Data Access layer. It is not MVVM, you are missing ViewModel. The view binds to properties on a ViewModel, which, in turn, exposes data contained in model objects and other state specific to the view. The bindings between view and ViewModel are simple to construct because a ViewModel object is set as the DataContext of a view. If property values in the ViewModel change, those new values automatically propagate to the view via data binding. When the user clicks a button in the View, a command on the ViewModel executes to perform the requested action.
我建议您参考以下链接:
I suggest you refer the links below:
#Patterns-具有Model-View-ViewModel设计模式的WPF应用
#Patterns - WPF Apps With The Model-View-ViewModel Design Pattern
https://msdn.microsoft.com/en-us/magazine/dd419663.aspx
# WPF MVVM逐步(从基础到高级)
#WPF MVVM step by step (Basics to Advance Level)
这篇关于WPF中的MVVM模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!