WPF MVVM模式開發簡明實現教程 7 DevExpress MVVM


WPF MVVM模式開發實現簡明教程 1 開篇簡介 

WPF MVVM模式開發實現簡明教程 2 初識 INotifyPropertyChanged

WPF MVVM模式開發簡明實現教程 3 事件綁定   

WPF MVVM模式開發實現簡明教程 3-1 BaseCommand  

WPF MVVM模式開發實現簡明教程 4 ViewModelBase  

WPF MVVM模式開發簡明實現教程 5 使用MultiValueConverter進行多參數事件綁定 

WPF MVVM模式開發簡明實現教程 6 其他綁定  

WPF MVVM模式開發簡明實現教程 7 DevExpress MVVM  

WPF MVVM模式開發簡明實現教程 8 完結 附全部代碼  

 

可以看到比原生的用法簡單很多,POCO View 結合 DXCommand很方便

自己其實也可以實現

 

ViewModelBase

View

<Button Command="{Binding LoginCommand}">Login</Button>
        <TextBlock Text="{Binding Status}" />

ViewModel

public string Status {
            get { return GetValue<string>(); }
            private set { SetValue(value); }
        }

        [Command]
        public void Login() {
            Status = "User: " + UserName;
        }
        public bool CanLogin() {
            return !string.IsNullOrEmpty(UserName);
        }

 

POCO View

View

<Button Command="{Binding LoginCommand}">Login</Button>
        <TextBlock Text="{Binding Status}" />

ViewModel

        public virtual string Status { get; protected set; }

        // LoginCommand will be created for the Login and CanLogin methods: 
        public void Login() {
            Status = "User: " + UserName;
        }
        public bool CanLogin() {
            return !string.IsNullOrEmpty(UserName);
        }

  

DelegateCommand

view

<dxlc:LayoutControl Orientation="Vertical" VerticalAlignment="Top">
        <dxlc:LayoutGroup Header="Command without parameter" Orientation="Vertical" View="GroupBox">
            <CheckBox IsChecked="{Binding CanExecuteSaveCommand}" Content="Can Save"/>
            <Button Command="{Binding SaveCommand}">Save</Button>
        </dxlc:LayoutGroup>
        <dxlc:LayoutGroup Header="Command with parameter" Orientation="Vertical" View="GroupBox">
            <dxe:TextEdit Text="{Binding FileName, UpdateSourceTrigger=PropertyChanged}" NullText="Enter file name"/>
            <Button Command="{Binding OpenCommand}" CommandParameter="{Binding FileName}">Open</Button>
        </dxlc:LayoutGroup>
    </dxlc:LayoutControl>

ViewModel

public ICommand SaveCommand { get; private set; }
        public ICommand OpenCommand { get; private set; }

        public DelegateCommandsViewModel() {
            CanExecuteSaveCommand = true;

            SaveCommand = new DelegateCommand(
                () => MessageBox.Show("Action: Save"),
                () => CanExecuteSaveCommand);

            OpenCommand = new DelegateCommand<string>(
                fileName => MessageBox.Show(string.Format("Action: Open {0}", FileName)),
                fileName => !string.IsNullOrEmpty(fileName));
        } 

        public bool CanExecuteSaveCommand {
            get { return GetValue<bool>(); }
            set { SetValue(value); }
        }
        public string FileName {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }

  

DXCommand

View

<dxg:GridControl MouseDoubleClick="{DXEvent 'GridControl_MouseDoubleClick(@sender, @args)'}" >

  

ViewModel

public void GridControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var row = (sender as GridControl).CurrentItem;
            if (!(row is CreateIncidentInfomationModel createIncidentInfoModel))
            {
                return;
            }

        }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM