2017-07-28 19 views
0

我正嘗試將xaml.cs中的datepicker加載事件移至查看模型中。我不想在xaml.cs中隱藏代碼。下面給出若要將xaml.cs中的datepicker加載事件更改爲xaml wpf中的viewmodel中繼命令(使用mvvm模式)

XAML代碼,

<DatePicker BorderBrush="LightBlue" BorderThickness="1" 
Name="dtTierValidTo" IsEnabled="{Binding CanEdit}" 
DisplayDateStart="{Binding TierDealValidFromDate, Mode=OneWay}" 
SelectedDate="{x:Static sys:DateTime.Now}" 
DisplayDate="{x:Static sys:DateTime.Now}" Width="120" HorizontalAlignment="Left"> 
<i:Interaction.Triggers> 
    <i:EventTrigger EventName="Loaded" > 
    <cmd:EventToCommand Command="{Binding DtTierValidTo_Loaded}" 
    PassEventArgsToCommand="True" /> 
    </i:EventTrigger> 
</i:Interaction.Triggers> 
</DatePicker> 

ViewModel.cs代碼如下..

public RelayCommand DtTierValidTo_Loaded { get; protected set; } 
DtTierValidTo_Loaded = new RelayCommand(dtTierValidTo_Loaded); 

private void dtTierValidTo_Loaded() 
{ 
DatePicker dtTierValidTo = new DatePicker(); 
//DatePicker dtTierValidTo = sender as DatePicker; 
dtTierValidTo.BlackoutDates.AddDatesInPast(); 
} 

但問題是,在沒有得到觸發事件。

我能來到這裏

+0

根據您的要求,我從代碼了。遵循這個鏈接你的任務將完成https://stackoverflow.com/questions/ 1638128/how-to-bind-blackoutdates-in-wpf-toolkit-calendar-control – Ramankingdom

+0

如何將該類附加到xaml。對不起,我對WPF xaml很陌生MVVM –

+0

它會進入代碼背後的主類。參考https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-register-an-attached-property – Ramankingdom

回答

0

一些幫助試試這個辦法

<i:Interaction.Triggers> 
        <i:EventTrigger EventName="Loaded"> 
         <i:InvokeCommandAction Command="{Binding DtTierValidTo_Loaded}"/> 
        </i:EventTrigger> 
</i:Interaction.Triggers> 
+0

對不起,它不適合我。有沒有其他簡單的方法來實現這個 –

+0

我如何傳遞sender對象到relaycommand DtTierValidTo_Loaded –

相關問題