一、 动态窗口
方法1:this方法
this.Width = 800;
this.Height=800;
方法2:命令参数CommandParameter,传递当前窗口Sendu.xaml
https://blog.csdn.net/qq_42564846/article/details/83306373
将UserControl.xmal作为一个对象传递,然后在下面的代码修改该对象的长度和宽度:
二、WPF下如何让控件大小跟着窗口的大小变化而变化
控件的宽度、高度都跟随着Window的长度和宽度
<Window x:Name="window" x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Rectangle x:Name="rectangle" Fill="#FF53A8F1" Width="{Binding ActualWidth, ElementName=window, Mode=OneWay}"
Height="{Binding ActualHeight, ElementName=window, Mode=OneWay}" />
</Grid>
</Window>
效果:
三、一个控件绑定另一个控件的大小
<Grid VerticalAlignment="Bottom" d:LayoutOverrides="Height">
<Border BorderBrush="Red" BorderThickness="1"
Width="{Binding Width, ElementName=textBlock, Mode=OneWay}"
Height="{Binding Height, ElementName=textBlock}"/>
<TextBlock x:Name="textBlock" Text="首都开罗解放立刻建立联防队" Width="200" Height="30"/>
</Grid>
四、代码
Sendu.xaml
<UserControl x:Name="SenduName"
x:Class="KeenRay.Modules.History.Views.Sendu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cc="clr-namespace:KeenRay.Controls;assembly=KeenRay.Controls"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:KeenRay.Modules.History.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Height="160" Width="483">
<Grid Name="rootGrid" Background="#dbdce1">
<!--Height="{Binding SenduHeight,Mode=TwoWay}"-->
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="50"/>
<RowDefinition Name="hidedRow1" Height="0"/>
<RowDefinition />
</Grid.RowDefinitions>
<Canvas Grid.Row="0">
<Label Canvas.Left="40" Content="选项:"></Label>
</Canvas>
<Canvas Grid.Row="1">
<RadioButton Name="radioImport" Content="导入" Canvas.Left="109" Canvas.Top="18" IsChecked="{Binding RadioImportIsChecked,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding radioImportCheckedCommand}" CommandParameter="{Binding ElementName=SenduName}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Name="radioExport" Content="导出" Canvas.Left="300" Canvas.Top="18" IsChecked="{Binding RadioExportIsChecked,Mode=TwoWay}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding radioExportCheckedCommand}" CommandParameter="{Binding ElementName=SenduName}"/>
<!--<i:InvokeCommandAction Command="{Binding radioExportCheckedCommand}"/>-->
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</Canvas>
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="40"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Canvas Grid.Row="0">
<Label Content="导出选项:" Canvas.Left="40" Canvas.Top="6"/>
</Canvas>
<Canvas Grid.Row="1">
<RadioButton Name="radioExportCurSelect" Canvas.Left="109" Canvas.Top="15" Content="导出当前选择检查" IsChecked="{Binding radioExportCurSelectIsChecked,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding radioExportCurSelectCheckedCommand}" CommandParameter="{Binding ElementName = radioExportCurSelect}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
<RadioButton Name="radioExportDateRange" Canvas.Left="300" Canvas.Top="15" Content="导出时间段检查" IsChecked="{Binding radioExportDateRangeIsChecked,Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding radioExportDateRangeCheckedCommand}" CommandParameter="{Binding ElementName = radioExportDateRange}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</Canvas>
<Canvas Grid.Row="2">
<DatePicker Name="dpkDateStart" Style="{StaticResource ExPortDatePickerStyle}" Canvas.Left="109" Width="150" IsEnabled="{Binding dpkDateStartIsEnabled}">
<i:Interaction.Triggers>
<!--默认加载-->
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding dpkDateStartLoadedCommand}" CommandParameter="{Binding ElementName = dpkDateStart}"/>
</i:EventTrigger>
<!--选中日期-->
<i:EventTrigger EventName="SelectedDateChanged">
<i:InvokeCommandAction Command="{Binding dpkDateStartSelectedDateChangedCommand}" CommandParameter="{Binding SelectedDate,ElementName = dpkDateStart,Mode=TwoWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DatePicker>
<DatePicker Name="dpkDateEnd" Style="{StaticResource ExPortDatePickerStyle}" Canvas.Left="300" Width="150" IsEnabled="{Binding dpkDateEndIsEnabled}">
<i:Interaction.Triggers>
<!--默认加载-->
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding dpkDateEndLoadedCommand}" CommandParameter="{Binding ElementName = dpkDateEnd}"/>
</i:EventTrigger>
<!--选中日期-->
<i:EventTrigger EventName="SelectedDateChanged">
<i:InvokeCommandAction Command="{Binding dpkDateEndSelectedDateChangedCommand}" CommandParameter="{Binding SelectedDate,ElementName = dpkDateEnd,Mode=TwoWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DatePicker>
</Canvas>
</Grid>
<Canvas Grid.Row="3">
<cc:ImageButton Content="{DynamicResource History.PrintSetting.Operate.Content}" Width="100" Height="30" Canvas.Right="200" Canvas.Left="246" Canvas.Top="5" FontSize="18" IsDefault="True" Foreground="#606467" DefaultImage="{StaticResource Public.EmptyButton122x52.Enable.Icon}" PressedImage="{StaticResource Public.EmptyButton112x52.Touches.Icon}" Command="{Binding OperateCommand}"></cc:ImageButton>
<cc:ImageButton Content="{DynamicResource History.PrintSetting.Cancel.Content}" Width="100" Height="30" Canvas.Right="5" Canvas.Left="351" Canvas.Top="5" FontSize="18" Foreground="#606467" DefaultImage="{StaticResource Public.EmptyButton122x52.Enable.Icon}" PressedImage="{StaticResource Public.EmptyButton112x52.Touches.Icon}" Command="{Binding CancelCommand}"></cc:ImageButton>
</Canvas>
</Grid>
</UserControl>
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 属性集
/// <summary>
/// 导入选中
/// </summary>
public bool RadioImportIsChecked
{
get;
set;
}
/// <summary>
/// 导出选中
/// </summary>
public bool RadioExportIsChecked
{
get;
set;
}
/// <summary>
/// 导出当前选择检查,选中
/// </summary>
public bool radioExportCurSelectIsChecked
{
get;
set;
}
/// <summary>
/// 导出时间段检查,选中
/// </summary>
public bool radioExportDateRangeIsChecked
{
get;
set;
}
/// <summary>
/// 日期起始时间,文本
/// </summary>
public string dpkDateStartText
{
get;
set;
}
/// <summary>
/// 日期结束时间,文本
/// </summary>
public string dpkDateEndText
{
get;
set;
}
/// <summary>
/// 起始日期是否允许操作
/// </summary>
public bool dpkDateStartIsEnabled
{
get;
set;
}
/// <summary>
/// 终止日期是否允许操作
/// </summary>
public bool dpkDateEndIsEnabled
{
get;
set;
}
#endregion
#region 命令集
/// <summary>
/// 导入
/// </summary>
public DelegateCommand<object> radioImportCheckedCommand { get; private set; }
/// <summary>
/// 导出
/// </summary>
//public DelegateCommand radioExportCheckedCommand { get; private set; }
public DelegateCommand<object> radioExportCheckedCommand { get; private set; }
/// <summary>
/// 导出当前选择检查
/// </summary>
public DelegateCommand<object> radioExportCurSelectCheckedCommand { get; private set; }
/// <summary>
/// 导出时间段检查
/// </summary>
public DelegateCommand<object> radioExportDateRangeCheckedCommand { get; private set; }
/// <summary>
/// 起始日期默认加载
/// </summary>
public DelegateCommand<object> dpkDateStartLoadedCommand { get; private set; }
/// <summary>
/// 起始日期选中
/// </summary>
public DelegateCommand<object> dpkDateStartSelectedDateChangedCommand { get; private set; }
/// <summary>
/// 终止日期默认加载
/// </summary>
public DelegateCommand<object> dpkDateEndLoadedCommand { get; private set; }
/// <summary>
/// 终止日期选中
/// </summary>
public DelegateCommand<object> dpkDateEndSelectedDateChangedCommand { get; private set; }
/// <summary>
/// 操作
/// </summary>
public DelegateCommand<object> OperateCommand { get; private set; }
/// <summary>
/// 取消
/// </summary>
public DelegateCommand<object> 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<PatientInfoHistroryModel> pipList = new List<PatientInfoHistroryModel>();
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;
}
/// <summary>
/// 关闭窗口
/// </summary>
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<object>(radioImport_Checked);
//radioExportCheckedCommand = new DelegateCommand(radioExport_Checked);
radioImportCheckedCommand = new DelegateCommand<object>(radioImport_Checked);
radioExportCheckedCommand = new DelegateCommand<object>(radioExport_Checked);
radioExportCurSelectCheckedCommand = new DelegateCommand<object>(radioExportCurSelect_Checked);
radioExportDateRangeCheckedCommand = new DelegateCommand<object>(radioExportDateRange_Checked);
dpkDateStartLoadedCommand = new DelegateCommand<object>(DpkDateStart_Loaded);
dpkDateStartSelectedDateChangedCommand = new DelegateCommand<object>(DpkDateStart_SelectedDateChanged);
dpkDateEndLoadedCommand = new DelegateCommand<object>(DpkDateEnd_Loaded);
dpkDateEndSelectedDateChangedCommand = new DelegateCommand<object>(DpkDateEnd_SelectedDateChanged);
OperateCommand = new DelegateCommand<object>(btnOperation_Click);
CancelCommand = new DelegateCommand<object>(CancelCommandFunc);
}
/// <summary>
/// 初始窗口的状态
/// </summary>
private void InitInfo()
{
RadioImportIsChecked = true;
radioExportCurSelectIsChecked = true;
m_nExportOperation = 0;
m_nCurOperationType = 0;
}
/// <summary>
/// 获取类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="depObj"></param>
/// <returns></returns>
private static T GetChildOfType<T>(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<T>(child);
if (result != null) return result;
}
return null;
}
/// <summary>
/// 结束日期选择
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
}
/// <summary>
/// 开始日期选择
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
}
/// <summary>
/// 结束日期控件加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DpkDateEnd_Loaded(object obj)
{
var dp = obj as DatePicker;
if (dp == null) return;
var tb = GetChildOfType<DatePickerTextBox>(dp);
if (tb == null) return;
var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
if (wm == null) return;
wm.Content = "终止日期";
}
/// <summary>
/// 开始日期控件加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DpkDateStart_Loaded(object obj)
{
var dp = obj as DatePicker;
if (dp == null) return;
var tb = GetChildOfType<DatePickerTextBox>(dp);
if (tb == null) return;
var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
if (wm == null) return;
wm.Content = "起始日期";
}
/// <summary>
/// 导入功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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();
}
/// <summary>
/// 导出功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//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();
}