编辑:我已经看过 Infragistics 论坛。下面的代码基于他们的示例。数据绑定似乎不起作用。
我认为我有一个 Infragistics XamDataGrid。
<DockPanel>
<grid:XamDataGrid x:Name="gridData" DataContext="{Binding Path=DataEditorDataTable}" DataSource="{Binding Path=DataEditorDataTable.DefaultView}"
IsSynchronizedWithCurrentItem="True" Visibility="{Binding DataGridVisible}">
<grid:XamDataGrid.FieldLayoutSettings>
<grid:FieldLayoutSettings AutoGenerateFields="True" AllowAddNew="False" AllowDelete="False" />
</grid:XamDataGrid.FieldLayoutSettings>
</grid:XamDataGrid>
</DockPanel>
我在构造函数中为用户控件设置数据上下文。
public DataEditor(SomeDataType DataType, IEventAggregator eventaggregator)
{
InitializeComponent();
this.DataContext = new DataEditorViewModel(DataType, eventaggregator);
}
在数据编辑器视图模型中,我订阅了一个让我知道数据何时发生变化的事件,我构建了一个数据表并调用了一个方法 SetData。(我无法预先知道网格中将显示多少列数据,并且这些列随着用户交互而不断变化,所以我希望使用数据表进行绑定。)
我以这样的方法分配属性。
/// <summary>
/// Returns the data that the data editor displays.
/// </summary>
public DataTable DataEditorDataTable
{
get
{
return dtDataEditor;
}
set
{
dtDataEditor = value;
OnPropertyChanged("DataEditorDataTable");
}
}
/// <summary>
/// Method to set data on load
/// </summary>
private void SetData(DataTable dtDataEditor)
{
if (!isDataEditorCellEdited)
if (dtDataEditor != null && dtDataEditor.Rows.Count > 0)
{
try
{
//Assign the data to the grid
DataEditorDataTable = dtDataEditor;
DataGridVisible = Visibility.Visible;
}
catch
{
//If any exception occurs, hide the grid
DataGridVisible = Visibility.Collapsed;
}
}
else
//If no data, hide the grid
DataGridVisible = Visibility.Collapsed;
}
问题是绑定根本没有发生。关于绑定,我有什么特别遗漏的吗?