C# 正在尝试更新DevExpress GridControl项资源上的绑定值
我在WPF中有一个DevExpress GridControl,其中有一个绑定的C# 正在尝试更新DevExpress GridControl项资源上的绑定值,c#,wpf,data-binding,devexpress,C#,Wpf,Data Binding,Devexpress,我在WPF中有一个DevExpress GridControl,其中有一个绑定的ItemsSource和列中的字段。当我初始化数据源中的值时,一切都正常,但当数据应该更新时,就不会了 我在用户控件中也有一个标签,它包含GridControl,并且可以很好地更新 因此,我的XAML是: <Grid> <Grid.RowDefinitions> <RowDefinition Height="250" /> <RowDe
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; }
}
class ParamDataSource:ViewModelBase//使用上面的DevExpress.Mvvm
{
公共数据源()
{
//这些东西放在网格上没问题。
ObservableParams=new System.Collections.ObjectModel.ObservableCollection
{
新参数表行
{
ParamName=“Param1”,
参数值=0
},
新参数表行
{
ParamName=“Param2”,
参数值=0
},
新参数表行
{
ParamName=“Param3”,
最佳值=0
}
};
LabelText=“立即开始”;
}
公共ObservableCollection ObservableParams{get;set;}
公共字符串LabelText{get;set;}
public void UpdateParam(int-paramIndex,十进制?paramValue)
{
ObservableParams[paramIndex].ParamValue=ParamValue;
RaisePropertyChanged(“可观察到的图表”);
//此标签在视图上更新得很好,但参数值不更新。。。
LabelText=string.Format(“Done Param{0}”,paramIndex);
RaisePropertyChanged(“LabelText”);
}
}
公共类参数表行
{
公共字符串ParamName{get;set;}
公共十进制?参数值{get;set;}
}
只需在模型类上实现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。很好,看起来已经完成了,出于某种原因,我认为是paramdasource
必须实现INotifyPropertyChanged
,而这是由ViewModelBase
负责的。..@colmdeViewModelBase
只是继承的类,让您有机会利用RaisePropertyChanged(string propertyName)
。但是,您没有使用该方法,因此不会发生任何事情。只需在属性设置器中写入RaisePropertyChanged(string propertyName)
,模型中的所有数据都将更新。