WPF 基础(四)WPF 绑定方式实现动态窗口:传递一个窗口、控件宽度绑定了窗口宽度、一个控件绑定另一个控件的大小。

一、 动态窗口

方法1:this方法

this.Width = 800;
this.Height=800;

方法2:命令参数CommandParameter,传递当前窗口Sendu.xaml

https://blog.csdn.net/qq_42564846/article/details/83306373

WPF 基础(四)WPF 绑定方式实现动态窗口:传递一个窗口、控件宽度绑定了窗口宽度、一个控件绑定另一个控件的大小。_第1张图片

将UserControl.xmal作为一个对象传递,然后在下面的代码修改该对象的长度和宽度:

WPF 基础(四)WPF 绑定方式实现动态窗口:传递一个窗口、控件宽度绑定了窗口宽度、一个控件绑定另一个控件的大小。_第2张图片

 

二、WPF下如何让控件大小跟着窗口的大小变化而变化

控件的宽度、高度都跟随着Window的长度和宽度





效果:

WPF 基础(四)WPF 绑定方式实现动态窗口:传递一个窗口、控件宽度绑定了窗口宽度、一个控件绑定另一个控件的大小。_第3张图片

三、一个控件绑定另一个控件的大小


      
      

四、代码

 Sendu.xaml


    
    
        
        
            
            
            
            
        
        
            
        

        
            
                
                    
                        
                    
                
            
            
                
                    
                        
                        
                    
                
            
        

        
            
                
                
                
            
            
                
            
                
                    
                        
                            
                        
                    
                
                
                    
                        
                            
                        
                    
                
            
            
                
                    
                        
                        
                            
                        
                        
                        
                            
                        
                    
                

                
                    
                        
                        
                            
                        

                        
                        
                            
                        
                    
                
            
        
        
            
            
        
    


SenduViewModel.cs(部分代码):

using KeenRay.Common;
using KeenRay.Controls;
using KeenRay.Modules.History.Models;
using KeenRay.Modules.History.Notifications;
using Prism.Commands;
using Prism.Interactivity.InteractionRequest;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;


namespace KeenRay.Modules.History.ViewModels
{
    public class SenduViewModel : BindableBase, IInteractionRequestAware
    {
        private SenduNotification senduNotification;
        #region 属性集
        /// 
        /// 导入选中
        /// 
        public bool RadioImportIsChecked
        {
            get;
            set;
        }
        /// 
        /// 导出选中
        /// 
        public bool RadioExportIsChecked
        {
            get;
            set;
        }
        /// 
        /// 导出当前选择检查,选中
        /// 
        public bool radioExportCurSelectIsChecked
        {
            get;
            set;
        }
        /// 
        /// 导出时间段检查,选中
        /// 
        public bool radioExportDateRangeIsChecked
        {
            get;
            set;
        }

        /// 
        /// 日期起始时间,文本
        /// 
        public string dpkDateStartText
        {
            get;
            set;
        }

        /// 
        /// 日期结束时间,文本
        /// 
        public string dpkDateEndText
        {
            get;
            set;
        }

        /// 
        /// 起始日期是否允许操作
        /// 
        public bool dpkDateStartIsEnabled
        {
            get;
            set;
        }

        /// 
        /// 终止日期是否允许操作
        /// 
        public bool dpkDateEndIsEnabled
        {
            get;
            set;
        }
        #endregion

        #region 命令集
        /// 
        /// 导入
        /// 
        public DelegateCommand radioImportCheckedCommand { get; private set; }
        /// 
        /// 导出
        /// 
        //public DelegateCommand radioExportCheckedCommand { get; private set; }
        public DelegateCommand radioExportCheckedCommand { get; private set; }
        /// 
        /// 导出当前选择检查
        /// 
        public DelegateCommand radioExportCurSelectCheckedCommand { get; private set; }
        /// 
        /// 导出时间段检查
        /// 
        public DelegateCommand radioExportDateRangeCheckedCommand { get; private set; }
        /// 
        /// 起始日期默认加载
        /// 
        public DelegateCommand dpkDateStartLoadedCommand { get; private set; }
        /// 
        /// 起始日期选中
        /// 
        public DelegateCommand dpkDateStartSelectedDateChangedCommand { get; private set; }
        /// 
        /// 终止日期默认加载
        /// 
        public DelegateCommand dpkDateEndLoadedCommand { get; private set; }

        /// 
        /// 终止日期选中
        /// 
        public DelegateCommand dpkDateEndSelectedDateChangedCommand { get; private set; }

        /// 
        /// 操作
        /// 
        public DelegateCommand OperateCommand { get; private set; }
        /// 
        /// 取消
        /// 
        public DelegateCommand CancelCommand { get; private set; }

        #endregion

        //导入导出功能记录常量
        private const int EXPORT_MODE = 0;
        private const int IMPORT_MODE = 1;

        //记录当前操作
        private int m_nCurOperationType;
        private int m_nExportOperation;

        //导出时间
        DateTime? dtEnd = null;
        //DateTime dtEnd;
        DateTime? dtStart = null;

        //接收导出的病人列表
        List pipList = new List();

        string m_strPrecondition = "PatientInfo_Table.PatientKey=PatientStudy_Table.PatientKey";
        public INotification Notification
        {
            get
            {
                return this.senduNotification;
            }
            set
            {
                if (value is SenduNotification)
                {
                    this.senduNotification = value as SenduNotification;
                    this.RaisePropertyChanged("Notification");
                }
            }
        }

        public Action FinishInteraction
        {
            get; set;
        }

        /// 
        /// 关闭窗口
        /// 
        public void CancelCommandFunc(object obj)
        {
            if (this.FinishInteraction != null)
                this.FinishInteraction();
        }

        public SenduViewModel()
        {
            //WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //InitializeComponent();
            InitInfo();
            //赋值
            //pipList.Clear();
            //foreach (PatientInfoHistroryModel tpip in TempList)
            //{
            //    pipList.Add(tpip);
            //}

            //radioImportCheckedCommand = new DelegateCommand(radioImport_Checked);
            //radioExportCheckedCommand = new DelegateCommand(radioExport_Checked);
            radioImportCheckedCommand = new DelegateCommand(radioImport_Checked);
            radioExportCheckedCommand = new DelegateCommand(radioExport_Checked);
            radioExportCurSelectCheckedCommand = new DelegateCommand(radioExportCurSelect_Checked);
            radioExportDateRangeCheckedCommand = new DelegateCommand(radioExportDateRange_Checked);
            dpkDateStartLoadedCommand = new DelegateCommand(DpkDateStart_Loaded);
            dpkDateStartSelectedDateChangedCommand = new DelegateCommand(DpkDateStart_SelectedDateChanged);
            dpkDateEndLoadedCommand = new DelegateCommand(DpkDateEnd_Loaded);
            dpkDateEndSelectedDateChangedCommand = new DelegateCommand(DpkDateEnd_SelectedDateChanged);
            OperateCommand = new DelegateCommand(btnOperation_Click);
            CancelCommand = new DelegateCommand(CancelCommandFunc);
        }

        /// 
        /// 初始窗口的状态
        /// 
        private void InitInfo()
        {
            RadioImportIsChecked = true;
            radioExportCurSelectIsChecked = true;
            m_nExportOperation = 0;
            m_nCurOperationType = 0;
        }



        /// 
        /// 获取类型
        /// 
        /// 
        /// 
        /// 
        private static T GetChildOfType(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj == null) return null;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                var child = VisualTreeHelper.GetChild(depObj, i);

                var result = (child as T) ?? GetChildOfType(child);
                if (result != null) return result;
            }
            return null;
        }

        /// 
        /// 结束日期选择
        /// 
        /// 
        /// 
        private void DpkDateEnd_SelectedDateChanged(object obj)
        {
            //dtEnd = this.dpkDateEnd.SelectedDate;
            //var dtEnd = obj;
            dtEnd = (DateTime)obj;
            //DatePicker aa = obj as DatePicker;
            //string bb = aa.Text;
            //DateTime dd = (DateTime)aa.SelectedDate;

            //if (this.dpkDateStart.Text != null && this.dpkDateStart.Text != "")
            if (dtEnd.ToString() != null && dtEnd.ToString() != "")
            {
                try
                {
                    dtStart = DateTime.Parse(dtEnd.ToString());
                    if (dtStart > dtEnd)
                    {
                        EMessageBox.Show("导出终止日期不能早于起始日期!", "", EMessageBoxButton.OK, EMessageBoxImage.Warning);
                        dtEnd = null;
                    }
                }
                catch (Exception ex)
                {
                    EMessageBox.Show("日期格式错误!", "", EMessageBoxButton.OK, EMessageBoxImage.Error);
                    //m_cGlobalData.m_cLogs.WriteFaultLog("ExInPortDlg::DpkDateStart_SelectedDateChanged--  日期格式错误!" + ex.Message);
                }
            }
        }

        /// 
        /// 开始日期选择
        /// 
        /// 
        /// 
        private void DpkDateStart_SelectedDateChanged(object obj)
        {
            dtStart = (DateTime)obj;

            if (dtStart.ToString() != null && dtStart.ToString() != "")
            {
                try
                {
                    dtEnd = DateTime.Parse(dtStart.ToString());
                    if (dtStart > dtEnd)
                    {
                        EMessageBox.Show("导出起始日期不能晚于终止日期!", "", EMessageBoxButton.OK, EMessageBoxImage.Warning);
                        dtStart = null;
                    }
                }
                catch (Exception ex)
                {
                    EMessageBox.Show("日期格式错误!", "", EMessageBoxButton.OK, EMessageBoxImage.Error);
                    //m_cGlobalData.m_cLogs.WriteFaultLog("ExInPortDlg::DpkDateStart_SelectedDateChanged--  日期格式错误!" + ex.Message);
                }
            }
        }

        /// 
        /// 结束日期控件加载事件
        /// 
        /// 
        /// 
        private void DpkDateEnd_Loaded(object obj)
        {
            var dp = obj as DatePicker;
            if (dp == null) return;

            var tb = GetChildOfType(dp);
            if (tb == null) return;

            var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
            if (wm == null) return;

            wm.Content = "终止日期";
        }

        /// 
        /// 开始日期控件加载事件
        /// 
        /// 
        /// 
        private void DpkDateStart_Loaded(object obj)
        {
            var dp = obj as DatePicker;
            if (dp == null) return;

            var tb = GetChildOfType(dp);
            if (tb == null) return;

            var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
            if (wm == null) return;

            wm.Content = "起始日期";
        }

        /// 
        /// 导入功能
        /// 
        /// 
        /// 
        private void radioImport_Checked(object obj)
        {
            m_nCurOperationType = IMPORT_MODE;
            RadioImportIsChecked = false;
            RadioExportIsChecked = true;

            UserControl userControl = obj as UserControl;
            userControl.Height = userControl.Height -110;

            FoldCalendar();
        }

        /// 
        /// 导出功能
        /// 
        /// 
        /// 
        //private void radioExport_Checked()
        private void radioExport_Checked(object obj)
        {
            m_nCurOperationType = EXPORT_MODE;
            RadioImportIsChecked = false;
            RadioExportIsChecked = true;

            UserControl userControl = obj as UserControl;
            if (userControl.Height < 270)
            {
                userControl.Height = userControl.Height + 110;
            }
            UnfoldCalendar();
        } 
  

 

你可能感兴趣的:(----------,【基础】)