DevExpress WPF MVVM入门例子

设置界面(MainWindows.XAML)

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfRobot"
        xmlns:dxwui="http://schemas.devexpress.com/winfx/2008/xaml/windowsui" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:lcub="http://schemas.arction.com/bindablecharting/ultimate/" xmlns:dxca="http://schemas.devexpress.com/winfx/2008/xaml/carousel" xmlns:dxn="http://schemas.devexpress.com/winfx/2008/xaml/navbar"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
        xmlns:dxnav="http://schemas.devexpress.com/winfx/2008/xaml/navigation"
        xmlns:lcusb="http://schemas.arction.com/semibindablecharting/ultimate/"
        x:Class="WpfRobot.MainWindow"
        mc:Ignorable="d"
        WindowStyle="SingleBorderWindow" Height="528.8" Width="645.6">
    
    <DockPanel>
         <DockPanel.DataContext>
            <dxmvvm:ViewModelSource Type="local:ViewModel01"></dxmvvm:ViewModelSource>
        </DockPanel.DataContext> 

        <dxn:NavBarControl DockPanel.Dock="Right" Width="231" EachGroupHasSelectedItem="False" Background="#FFEDE7E7" GroupDescription="的方法" AllowSelectItem="False">
            <dxn:NavBarControl.View>
                <dxn:NavigationPaneView GroupDisplayMode="Text" ItemDisplayMode="ImageAndText" Orientation="Vertical" IsExpandButtonVisible="True" IsExpanded="True" ActiveGroupMinHeight="100"  FlowDirection="RightToLeft" ItemForeground="#FF4E3E3E" />
            </dxn:NavBarControl.View>
            <dxn:NavBarGroup x:Name="navBarGroup3" Header="系统设置" DisplayMode="ImageAndText" ImageSource="{dx:DXImage Image=Cancel_32x32.png}" >
                <dxn:NavBarItem x:Name="navBarItem4" >
                    <Button x:Name="mybutton" Content="{Binding Path=Val, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  />
                </dxn:NavBarItem>
                <dxn:NavBarItem x:Name="navBarItem5" >
                    <Button x:Name="mybutton2" Content="dsdsdsd"   Command="{Binding AddCommand}"/>
                </dxn:NavBarItem>            
            </dxn:NavBarGroup>
       </dxn:NavBarControl>
       </DockPanel>
</Window>

注意:

  • 按界面和业务分离的原则,不要在界面后台代码(MainWindow.Xaml.cs)中添加业务逻辑代码。
  • 每个UI标签都是一个UI对象,每个对象都有一个DataContext用来指定MVVM数据源。
  • 这里我们指定的就一个类型为ViewModel01的类。

创建ViewModel类(ViewModel01.cs)

namespace WpfRobot
{
   public class ViewModel01
    {
        public bool IsOK { get; set; }
        public virtual int Val { get; set; } = 100;

        public  void Add()
        {
            Val++;
        }
         public bool CanAdd()
        {
            return Val < 110;
        }
    }
}

注意:

  • 需要绑定的属性加virtual关键词(so easy,不用写那一堆INotifyCollectionChanged了)
  • 定义一个Add方法作为Command
  • 定义是否能执行Add方法的判断方法CanAdd
发布了30 篇原创文章 · 获赞 4 · 访问量 7461

猜你喜欢

转载自blog.csdn.net/guo1wu3shi4/article/details/104457673