2016-08-16 9 views
6

DataTemplate 2つのプロパティにバインドされた2 DatePickersの列があります。これらのコントロールのデータが変更されたとき、私はスタートとによってのみが更新されるため、両方を更新した場合にのみ、最後の制御は、この場合データテンプレート列の最後のコントロールのみが更新されます

 <sdk:DataGridTemplateColumn Width="300" CanUserReorder="False" >    
     <sdk:DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
       <Grid MouseRightButtonDown="ActionsGrid_MouseRightButtonDown" Width="300" Height="40" MouseLeftButtonDown="ActionsGrid_MouseLeftButtonDown"> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock VerticalAlignment="Stretch" Width="100" Text="{Binding Start, Converter={StaticResource DateConverter}}" 
             Padding="2" HorizontalAlignment="Center" /> 
          <TextBlock VerticalAlignment="Stretch" Width="100" Text="{Binding Due, Converter={StaticResource DateConverter}}" 
             Padding="2" HorizontalAlignment="Center" /> 
         </StackPanel> 
        </Grid> 
       </DataTemplate>             
      </sdk:DataGridTemplateColumn.CellTemplate> 
    <sdk:DataGridTemplateColumn.CellEditingTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <sdk:DatePicker VerticalAlignment="Top" Width="100" SelectedDate="{Binding Start, Mode=TwoWay,}" Padding="2" /> 
         <sdk:DatePicker VerticalAlignment="Top" Width="100" SelectedDate="{Binding Due, Mode=TwoWay, ValidatesOnDataErrors=True}" Padding="2" /> 
        </StackPanel> 
       </DataTemplate> 
    </sdk:DataGridTemplateColumn.CellEditingTemplate> 
    </sdk:DataGridTemplateColumn> 

に更新されます。また、結合作品罰金、私は私のモデルクラスでスタートにブレークポイントを入れてしまうと、それはヒットが、渡された値は、私が見つけたいくつかのデバッグした後にスタート

の元の値

EDIT 1

あります場合は、その私は1つのコントロールを私の内部に持っていますDataTemplateそれはうまく動作します。また、私は日付を変更するときブレークポイントはすぐにヒットします。しかし、私が複数のコントロールを持っている場合、ブレークポイントは、カラムから外に出てから最後のバインディングだけが機能するまで、ヒットしません。

EDIT 2

私は唯一のCellTemplateを使用して、細胞EditTemplate

 <sdk:DataGridTemplateColumn Width="300" CanUserReorder="False" >    
      <sdk:DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <sdk:DatePicker VerticalAlignment="Top" Width="100" SelectedDate="{Binding Start, Mode=TwoWay,}" Padding="2" /> 
         <sdk:DatePicker VerticalAlignment="Top" Width="100" SelectedDate="{Binding Due, Mode=TwoWay, ValidatesOnDataErrors=True}" Padding="2" /> 
        </StackPanel> 
       </DataTemplate> 
    </sdk:DataGridTemplateColumn.CellEditingTemplate> 
    </sdk:DataGridTemplateColumn> 

EDIT 3

private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e) 
     { 
      (sender as DatePicker).GetBindingExpression(DatePicker.SelectedDateProperty).UpdateSource(); 
     } 

を破棄した場合、それは正常に動作しますことに気づいたいくつかのmroeデバッグした後、私は両方のコントロールでバインディングをリフレッシュすることができましたselectedDatechangeイベントを受信し、送信者のバインディングを更新します。

2ウェイバインディングの作業がうまくいかない理由はまだわかりません。

これはなぜ起こっているのですか?

EDIT 4

モデルとプロパティ

public DateTime? Start 
     { 
      get { return _Start; } 
      set 
      { 
       _Start = value; Dirty = true; 

       if (_Start.HasValue && _Due.HasValue && _Start.Value > _Due.Value) 
        _dataErrors["Start"] = "Start date cannot be greater than the Due date"; 
       else 
        if (_dataErrors.ContainsKey("Start")) 
         _dataErrors.Remove("Start"); 
       NotifyPropertyChanged(); NotifyPropertyChanged("CalcStatus"); 
      } 
     } 
     public DateTime? Due 
     { 
      get { return _Due; } 
      set 
      { 
       _Due = value; Dirty = true; 
       if (_Start.HasValue && _Due.HasValue && _Start.Value > _Due.Value) 
        _dataErrors["Start"] = "Start date cannot be greater than the Due date"; 
       else 
        if (_dataErrors.ContainsKey("Start")) 
         _dataErrors.Remove("Start"); 
       NotifyPropertyChanged("Due"); NotifyPropertyChanged("CalcStatus"); 
      } 
     } 
+0

バインドされているビューモデルのプロパティを表示します。プロパティ変更通知を適切に実装していますか? – Nkosi

+0

はい私は、ビューモデルINotifyPropertyChangedイベントを実装しています。テンプレートにコントロールが1つしかない場合は、どのプロパティにもバインドされていてもヒットしています。 2つ以上のコントロールの場合、最後のコントロールのみが更新されます。変わったことは、別のセルまたは列を選択して最後のコントロールのみを更新するときにNotifyPropertyChangedが呼び出され、他のプロパティもヒットしますが、渡された値は既存の値と同じです。 –

+0

これがSilverlightの場合、WPFタグはなぜですか? :) BTWはUpdateSourceTrigger PropertyChangedをサポートしていますか? –

答えて

2

さてさて、私は私が最終的にそれを固定していると思います。私はまだそれが働いていなかった理由を100%確信していませんが、私は新しい小さなプロジェクトを作り、その理由を見つけようとします。

私はNoitifyPropertyChangedEventを変更して、[CallerMemberName]を渡して、それを呼び出すプロパティの名前が自動的に渡されるようにしました。つまり、「開始」などの名前プロパティを変更すると、更新することについて心配する必要はありません。NotifyPropertyChanged("Start")

public event PropertyChangedEventHandler PropertyChanged; 

     public void NotifyPropertyChanged([CallerMemberName] string propertyName =null) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

これは、DataTemplateColumnを除いたすべてのプロパティで問題なく機能しました。 標準コードに変換してpropertyNameを渡すと、私の問題が明示的に修正されました。

public event PropertyChangedEventHandler PropertyChanged; 

     public void NotifyPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
関連する問題