2016-06-17 9 views
0

CheckBoxと2つのTimePickerコントロールがあります。私はTimePicker2-TimePicker1> 0の違いがある場合にチェックボックスを有効にしたい。そうでなければ無効にする。条件付きチェックボックスを無効にする

XAMLで作成できますか?ここでは、コード

:あなたのViewModelで

<xctk:TimePicker Grid.Row="1" Grid.Column="0" Height="22" HorizontalAlignment="Stretch" 
           Format="ShortTime" Margin="8,0,0,0" 
           Value="{Binding ViewModel.StartTime,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
           ></xctk:TimePicker> 

<xctk:TimePicker Grid.Row="3" Grid.Column="0" Height="22" HorizontalAlignment="Stretch" 
           Format="ShortTime" Margin="8,0,0,0" 
           Value="{Binding ViewModel.EndTime,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
           ></xctk:TimePicker> 
<CheckBox Grid.Row="4" 
         HorizontalAlignment="Left" VerticalAlignment="Center" 
         Margin="8,0,0,0" 
        ??? IsEnabled="{Binding ElementName=}" 
         IsChecked="{Binding ViewModel.Date,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
         /> 

答えて

2

、UIに依存しない方法で日付の妥当性を表しゲッターのみのプロパティを作成します。 bool AreTimesValidのように。このプロパティのゲッターでは、開始時刻と終了時刻の差を評価し、それに応じてtrueまたはfalseを返します。

StartTimeおよびEndTimeプロパティが変更されるたびに、AreTimesValidプロパティのプロパティ変更通知を発生させます。

class YourViewModel : INotifyPropertyChanged 
{ 
    public bool AreTimesValid 
    { 
     get 
     { 
      return StartTime < EndTime; 
     } 
    } 

    public DateTime StartTime 
    { 
     get 
     { 
      return startTime; 
     } 

     set 
     { 
      startTime = value; 
      NotifyPropertyChanged("StartTime"); 
      NotifyPropertyChanged("AreTimesValid"); 
     } 
    } 

    private DateTime startTime; 

    public DateTime EndTime 
    { 
     get 
     { 
      return endTime; 
     } 

     set 
     { 
      endTime = value; 
      NotifyPropertyChanged("EndTime"); 
      NotifyPropertyChanged("AreTimesValid"); 
     } 
    } 

    private DateTime startTime; 
} 
はTHSにご CheckBox IsEnabledプロパティ AreTimesValidフィールドをバインドし

<CheckBox Grid.Row="4" 
    HorizontalAlignment="Left" VerticalAlignment="Center" 
    Margin="8,0,0,0" 
    IsEnabled="{Binding ElementName=ViewModel.AreTimesValid}"/> 

代替はIMultiValueConverterを実装するコンバータを使用し、そこに比較ロジックを置くことです。次に、直接バインドすることができます。しかし、私の意見では、最初の解決策はより単純で明確です。

関連する問題