how can i access datepicker under grid under datagridtemplate column in my xaml to my xaml.cs page?How will i get this

房东的猫 提交于 2019-12-11 15:51:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!