Devexpress Winform 使用MVVM


MVVM在WPF里很早就有了,在Winform里Devexpress最近幾個大版本才有的事,上一段代碼。

現在對話框上添加三個控件simpleButton1,simpleButton2,textEdit1,MvvmContext組件

public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        // 
        // POCO View Model provides out-of-the-box support of the INotifyPropertyChanged.
        // 
        public class ViewModel
        {
            // Bindable property will be created from this property.
            public virtual string Title { get; set; }
            // Just a method for readability
            public string GetTitleAsHumanReadableString()
            {
                if (Title == null)
                    return "(Null)";
                if (Title.Length == 0)
                    return "(Empty)";
                if (string.IsNullOrWhiteSpace(Title))
                    return "(WhiteSpace)";
                return Title;
            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            // Set type of POCO-ViewModel
            mvvmContext1.ViewModelType = typeof(ViewModel);
            // Data binding for the Title property (via MVVMContext API)
            mvvmContext1.SetBinding(textEdit1, c=>c.EditValue, "Title");
            // UI binding for the Report command
            ViewModel viewModel = mvvmContext1.GetViewModel<ViewModel>();
            simpleButton2.Click += (s, ee) => XtraMessageBox.Show(viewModel.GetTitleAsHumanReadableString());
            

        }

  當simpleButton1點擊執行后,simpleButton2點擊后顯示的就是textEdit1的值。


免責聲明!

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



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