2009-12-18 64 views
0

我的問題是在這裏: 我有一些類WPF Datagrid的不尊重項目屬性值的變化

public class Component 
{ 
    ... 
    private ServiceController service; 

    ... 
    public int ServiceStatus 
    { 
     get 
     { 
      switch(service.Status) 
      { 
       case ServiceControllerStatus.Stopped: 
        return 0; 

       case ServiceControllerStatus.Running: 
        return 1; 

       default: 
        return 2; 
      } 
     } 
    } 
    public void QueryService() 
    { 
     service.Refresh(); 
    } 
} 

和收集組件,在另一個類中聲明:

public class Motivation 
{ 
    // Downloaded data 
    ... 
    private ObservableCollection<Component> components; 
    public ObservableCollection<Component> Components 
    { 
     get { return components; } 
    } 

    public bool CheckServices() 
    { 
     bool changed = false; 
     foreach (Component C in components) 
     { 
      int prevStatus = C.ServiceStatus; 
      C.QueryService(); 

      if (prevStatus != C.ServiceStatus) 
       changed = true; 
     } 

     return changed; 
    } 

該組件列表中顯示的在WPF DataGrid中。我的想法:用於運行服務的綠色背景顏色,紅色 - 用於停止。工作正常,但只在開始。由定時器調用CheckServices(),如果返回值爲True,我想重新渲染我的網格,尊重新的服務狀態。這裏是XAML:

<Style x:Key="ServiceStateStyle" TargetType="z:DataGridRow"> 
     <Setter Property="Background" Value="Gray" /> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Path=ServiceStatus}" Value="0"> 
       <Setter Property="Background" Value="LightCoral" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding Path=ServiceStatus}" Value="1"> 
       <Setter Property="Background" Value="LightGreen" /> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
    <z:DataGrid Grid.Row="0" 
       Grid.ColumnSpan="4" 
       AutoGenerateColumns="False" 
       x:Name="DataGridComponents" 
       ItemContainerStyle="{DynamicResource ServiceStateStyle}"> 
       <z:DataGrid.Columns> 
         <z:DataGridTextColumn IsReadOnly="True" 
          Header="Component" Width="80" 
          Binding="{Binding Path=DisplayName}"/> 
        </z:DataGrid.Columns> 
       </z:DataGrid> 

我應該調用顯式的任何方法使DataGrid無效?我嘗試過使用InvalidateProperty,InvalidateVisual,GetBindingExpression(ItemContainerStyleProperty).UpdateTarget(),但沒有任何工作。誰能幫忙?

回答

0

Component類必須實現INotifyPropertyChanged,並在事件的某些屬性發生更改時引發該事件。

+0

它的工作!非常簡單。謝謝。 – Pavel 2009-12-18 08:08:04