2016-04-27 100 views
1

我在WPF中有一個DevExpress GridControl,其綁定的邊界爲ItemsSource和列中的字段。當我初始化數據源中的值時,一切正常,但是當數據應該更新時,它不會。嘗試更新DevExpress網格控件上的綁定值ItemsSource

我在用戶控件中也有一個標籤,其中包含GridControl並且更新正常。

所以我的XAML是:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="250" /> 
     <RowDefinition Height="3*" /> 
    </Grid.RowDefinitions> 

    <dxg:GridControl Grid.Row="0" x:Name="grid" DataContext="{StaticResource ParamDataSource}" ItemsSource="{Binding Path=ObservableParams}"> 
     <dxg:GridControl.Columns> 
      <dxg:GridColumn x:Name="ParamName" FieldName="ParamName" MinWidth="80" Width="80" AllowResizing="False" FixedWidth="True" Header="Parameter" /> 
      <dxg:GridColumn x:Name="ParamValue" Binding="{Binding ParamValue}" MinWidth="50" Width="50" SortIndex="0" Header="Best Value" /> 
     </dxg:GridControl.Columns> 

     <dxg:GridControl.View> 
      <dxg:TableView VerticalScrollbarVisibility="Hidden" x:Name="view" ShowGroupPanel="False" AllowFixedGroups="True" ShowGroupedColumns="False" AllowCascadeUpdate="False" AllowScrollAnimation="False" NavigationStyle="Row" AutoWidth="True" ShowFixedTotalSummary="False" /> 
     </dxg:GridControl.View> 
    </dxg:GridControl> 

    <Label DataContext="{StaticResource ParamDataSource}" Content="{Binding LabelText}" Margin="10, 10, 10, 10" Grid.Row="1"/> 
</Grid> 

然後對數據源的C#代碼...

class ParamDataSource : ViewModelBase // using DevExpress.Mvvm above 
{ 

    public ParamDataSource() 
    { 

// This stuff is put on the grid no problem. 

     ObservableParams = new System.Collections.ObjectModel.ObservableCollection<ParamTableRow> 
      { 
       new ParamTableRow 
       { 
        ParamName = "Param1", 
        ParamValue = 0 
       }, 

       new ParamTableRow 
       { 
        ParamName = "Param2", 
        ParamValue = 0 
       }, 

       new ParamTableRow 
       { 
        ParamName = "Param3", 
        BestValue = 0 
       } 
      }; 

     LabelText = "Starting Now"; 
    } 

    public ObservableCollection<ParamTableRow> ObservableParams { get; set; } 

    public string LabelText { get; set; } 

    public void UpdateParam(int paramIndex, decimal? paramValue) 
    { 
     ObservableParams[paramIndex].ParamValue = paramValue; 
     RaisePropertyChanged("ObservableParams"); 

// This label updates on the view just fine, but not the parameter values... 

     LabelText = string.Format("Done Param {0}", paramIndex); 
     RaisePropertyChanged("LabelText"); 
    } 
} 

public class ParamTableRow 
{ 
    public string ParamName { get; set; } 
    public decimal? ParamValue { get; set; } 
} 

回答

1

只實現INotifyPropertyChanged你的模型類:

public class ParamTableRow:INotifyPropertyChanged 
{ 
    private string paramName; 

    public string ParamName 
    { 
     get { return paramName; } 
     set { 
      paramName = value; 
      OnPropertyChanged("ParamName"); 
     } 
    } 

    private decimal? paramValue; 
    public decimal? ParamValue 
    { 
     get { return paramValue; } 
     set 
     { 
      paramValue = value; 
      OnPropertyChanged("ParamValue"); 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    }   
} 

原因ObservableCollection實現INotifyCollectionChanged,而不是INotifyPropertyChanged

INotifyCollectionChanged用於在添加項目或從項目集合中刪除項目時通知UI。

INotifyPropertyChanged用於通知UI何時將新值設置爲您的屬性。

+0

偉大的,看起來已經做到了這一點,出於某種原因,我認爲這是'ParamDataSource'必須實現'INotifyPropertyChanged',並由'ViewModelBase'照顧... – colmde

+0

@colmde'ViewModelBase'只是繼承類,並給你一個機會來利用'RaisePropertyChanged(string propertyName)'。但是,您沒有使用該方法,因此沒有任何反應。只需在你的屬性設置器中寫入'RaisePropertyChanged(string propertyName)',並且模型中的所有數據都將被更新。 – StepUp