问题
The below is my datepicker and I want to add this line of code in my xaml.cs
dtTierValidTo.BlackOutDates.AllDaysinPast();
the below is my datepicker code in xaml
<DatePicker BorderBrush="LightBlue" BorderThickness="1"
Name="dtTierValidTo" Opacity="false" IsEnabled="{Binding CanEdit}" DisplayDateStart="{Binding TierDealValidFromDate, Mode=TwoWay}"
SelectedDate="{x:Static sys:DateTime.Now}" DisplayDate="{x:Static sys:DateTime.Now}"
Width="120" HorizontalAlignment="Left">
</DatePicker>
my entire code is like below in xaml, the datepicker is in grid which is DataGridTemplateColumn
<DataGridTemplateColumn Width="140" Header="Tier Valid To">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TierDealValidToDateDisplayText}"/>
<!--<TextBlock Text="{Binding TierDealValidToDate}"></TextBlock>-->
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid>
<!--<DatePicker BorderBrush="LightBlue" BorderThickness="1"
Name="dtTierValidTo" IsEnabled="{Binding CanEdit}"
Text="{Binding TierDealValidToDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, TargetNullValue=''}"
DisplayDateStart="{Binding TierDealValidFromDate, Mode=OneWay}"
DisplayDate="{Binding TierDealValidToDate, Mode=OneWay}"
SelectedDate="{Binding TierDealValidToDate, Mode=OneWay}"
Width="120"
HorizontalAlignment="Left"></DatePicker>-->
<DatePicker BorderBrush="LightBlue" BorderThickness="1"
Name="dtTierValidTo" Opacity="false" IsEnabled="{Binding CanEdit}" DisplayDateStart="{Binding TierDealValidFromDate, Mode=TwoWay}"
SelectedDate="{x:Static sys:DateTime.Now}" DisplayDate="{x:Static sys:DateTime.Now}"
Width="120" HorizontalAlignment="Left">
<DatePicker.BlackoutDates>
<!--<CalendarDateRange Start="1/1/1500" End="{x:Static sys:DateTime.Now.AddDays(-1)}"/>-->
<CalendarDateRange Start="1/1/1500" End="7/23/2017"/>
</DatePicker.BlackoutDates>
</DatePicker>
<!--DisplayDateStart="{Binding TierDealValidFromDate, Mode=OneWay}"-->
<!--SelectedDate="{Binding TierDealValidToDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"-->
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
回答1:
just create an event of the datepicker loaded like below
in xaml
Name="datepickerName" Loaded="datepickerName_Loaded"
in xaml.cs
private void datepickerName_Loaded(object sender, RoutedEventArgs e) {
DatePicker dp = sender as DatePicker;
dp.BlackoutDates.AddDatesInPast();
}
来源:https://stackoverflow.com/questions/45266619/how-can-i-access-datepicker-under-grid-under-datagridtemplate-column-in-my-xaml