2016-06-25 52 views
0

我的約會系統的工作,我有一個包含DataTemplate中有一個切換按鈕,代表每個預約,如果預約確認的背景顏色應該改變一個列表框,這是我的列表框:WPF結合背景色不更新

添加一個新的約會時

public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     Brush _appointmentStatusBrush = null; 
     var fc = new BrushConverter(); 
     if (value == null) 
      return null; 
     Appointment _appointment = (Appointment)value; 
     switch (_appointment.Status) 
     { 
      case 0: 
       _appointmentStatusBrush = (Brush)fc.ConvertFrom("#FFFBF7CC"); 
       break; 
      case 1: 
       _appointmentStatusBrush = (Brush)fc.ConvertFrom("#FFFFFFFF"); 
       break; 
     } 
     return _appointmentStatusBrush; 
    } 

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new System.NotImplementedException(); 
} 

的結合的作品,但是當我更新現有的預約狀態,並刷新列表,顏色不更新!!:

<ListBox>      
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <ToggleButton Background="{Binding Converter={StaticResource AppointmentStatusColor}, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}"> 
        <StackPanel Orientation="Horizontal" Width="370"> 
         <TextBlock Text="{Binding AppointmentID}"/> 
         <TextBlock Text="{Binding Patient.FullName}"/> 
        </StackPanel> 
       </ToggleButton> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
</ListBox> 

背景屬性綁定到的IValueConverter

在此先感謝

+0

有INotifyPropertyChanged的實施? –

回答

-1

Background屬性應綁定到Appointment.Status財產與Mode=TwoWay

然後,當Status更改時,Converter將被調用。

所以,你的綁定應該像:Background="{Binding Status, Converter={StaticResource AppointmentStatusColor}, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"

+0

控件不會更改背景,所以這應該不重要。 –

+0

@AnjumSKhan,感謝您的回答,但是如果我不綁定它,那麼Converter如何調用呢! –

+0

@AbdulsalamElsharif你在'DataTemplate'的'Binding'中使用了'Converter'。看到我更新的答案。 – AnjumSKhan