尝试更新 DevExpress GridControl ItemsSource 上的绑定值
Trying to Update bound values on DevExpress GridControl ItemsSource
我在 WPF 中有一个 DevExpress GridControl,在列中有绑定 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; }
}
只需在您的模型上实施 INotifyPropertyChanged
class:
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。
我在 WPF 中有一个 DevExpress GridControl,在列中有绑定 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; }
}
只需在您的模型上实施 INotifyPropertyChanged
class:
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。