c# - 无论初始选定的日期值如何使两个 WPF DatePicker-s 以不同的宽度显示?
问题描述
当一个具有as和另一个初始化为not null时,两个DatePicker
控件最初显示为不同。Width
SelectedDate
null
DateTime
这里的问题陈述是 - 在用户将日期值输入到具有null
初始值的控件后,它Width
保持不变。
如何配置DatePicker
初始显示,使其大小相同,无论初始是否SelectedDate
为空
XAML 中有以下布局定义:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<DatePicker SelectedDate="{Binding Path=SelectedDate1, Mode=TwoWay}"/>
</Grid>
<Grid Grid.Row="1" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<DatePicker SelectedDate="{Binding Path=SelectedDate2, Mode=TwoWay}"/>
</Grid>
</Grid>
并且视图模型具有以下属性定义:
private DateTime? _selectedDate1;
private DateTime? _selectedDate2 = DateTime.Now;
public DateTime? SelectedDate1
{
get { return _selectedDate1; }
set
{
_selectedDate1 = value;
OnPropertyChanged(nameof(SelectedDate1));
}
}
public DateTime? SelectedDate2
{
get { return _selectedDate2; }
set
{
_selectedDate2 = value;
OnPropertyChanged(nameof(SelectedDate2));
}
}
解决方案
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition Height="35"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<DatePicker Grid.Row="0" Grid.Column="0"
SelectedDate="{Binding Path=SelectedDate1, Mode=TwoWay}"/>
<DatePicker Grid.Row="1" Grid.Column="0"
SelectedDate="{Binding Path=SelectedDate2, Mode=TwoWay}"/>
推荐阅读
- qt - 如何从主页文本字段为第二页文本字段设置值
- mysql - 处理(非)现有数据
- java - Android:从使用 MessagingStyle 的通知中获取消息
- angular - 以角度生成模块级站点地图
- javascript - Javascript拆分在Vue应用程序中不起作用
- r - R - 从字符串创建函数
- java - 在 Recycler Adapter 中获取活动 onDestroy()
- google-tag-manager - 无法为 Google 跟踪代码管理器添加 AMP 变量
- python - 在python nlp中匹配一组单词和一组句子
- php - 使用 Laravel 的带有 docker 映像的 Bitbucket 管道