【发布时间】:2017-08-01 11:15:28
【问题描述】:
此问题与https://stackoverflow.com/questions/42611351/cannot-bind-to-additional-properties-on-typed-datarow有关
我现在所做的是使用转换器来填充数据网格列,其中包含我从键入的数据行中的其他字段计算的值。初始更新一切正常。当我更改计算值所依赖的行值之一时,问题就出现了。我不知道如何更新计算的目标。
这里是 XAML...
<Window.Resources>
<local:FullNameConverter x:Key="FullNameConverter"/>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding TableA}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Width="30*" Header="Name" SortDirection="Ascending" />
<DataGridTextColumn Header="Value" Width="30*" Binding="{Binding Value}"/>
<DataGridTextColumn Header="Full Name" Width="30*"
Binding="{Binding Converter={StaticResource FullNameConverter}}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
这是后面的代码...
public partial class BindingBench : Window
{
SimplyDataSet _dataSet = new SimplyDataSet();
public BindingBench()
{
InitializeComponent();
_dataSet.TableA.Columns.Add(new DataColumn("FullName",typeof(string)));
SimplyDataSet.TableARow arow = _dataSet.TableA.AddTableARow("Z", 0.0);
arow = _dataSet.TableA.AddTableARow("X", 1.0);
arow = _dataSet.TableA.AddTableARow("Y", 2.0);
DataContext = this;
}
public DataTable TableA
{
get { return _dataSet.TableA; }
}
}
这是转换器...
public class FullNameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DataRowView rv = value as DataRowView;
if(rv != null)
{
SimplyDataSet.TableARow row = rv.Row as SimplyDataSet.TableARow;
if (row != null)
{
return row.Name + "=" + row.Value;
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
最后是类型化的数据集...
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="SimplyDataSet" targetNamespace="http://tempuri.org/SimplyDataSet.xsd" xmlns:mstns="http://tempuri.org/SimplyDataSet.xsd" xmlns="http://tempuri.org/SimplyDataSet.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:annotation>
<xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
<DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
<Connections />
<Tables />
<Sources />
</DataSource>
</xs:appinfo>
</xs:annotation>
<xs:element name="SimplyDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:EnableTableAdapterManager="true" msprop:Generator_DataSetName="SimplyDataSet" msprop:Generator_UserDSName="SimplyDataSet">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="TableA" msprop:Generator_TableClassName="TableADataTable" msprop:Generator_TableVarName="tableTableA" msprop:Generator_TablePropName="TableA" msprop:Generator_RowDeletingName="TableARowDeleting" msprop:Generator_RowChangingName="TableARowChanging" msprop:Generator_RowEvHandlerName="TableARowChangeEventHandler" msprop:Generator_RowDeletedName="TableARowDeleted" msprop:Generator_UserTableName="TableA" msprop:Generator_RowChangedName="TableARowChanged" msprop:Generator_RowEvArgName="TableARowChangeEvent" msprop:Generator_RowClassName="TableARow">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" msprop:Generator_ColumnVarNameInTable="columnName" msprop:Generator_ColumnPropNameInRow="Name" msprop:Generator_ColumnPropNameInTable="NameColumn" msprop:Generator_UserColumnName="Name" type="xs:string" minOccurs="0" />
<xs:element name="Value" msprop:Generator_ColumnVarNameInTable="columnValue" msprop:Generator_ColumnPropNameInRow="Value" msprop:Generator_ColumnPropNameInTable="ValueColumn" msprop:Generator_UserColumnName="Value" type="xs:double" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
注意事项:
- 这是一套完整的代码。您看不到 TableA 或 AddTableARow 实现的原因是有一个工具 (XSD) 可以生成代码来实现数据集。我也可以添加这个生成的代码,但即使对于像这个这样的小数据集,它也很庞大,而且不是很有帮助。
- 在我看来,我提交的这两个相关问题的基本问题是 DataGridColumn 通过 DataRowView 绑定到 DataRow。我正在向底层 DataRow 添加内容,但无法修改 DataRowView 的行为。因此,在我看来,DataSet 从根本上与 MVVM 不兼容。但是,我似乎找不到任何意见认为应该避免使用 DataSet 来支持其他东西。
- 我已经尝试了多种方法来更新计算单元格,包括 DataRowView.BeginEdit()/EndEdit()、在 DataRow 上实现 INotifyPropertyChanged 等。
- 我可以通过刷新数据源来更新计算列。这不是解决方案!我可以忍受更新一行中的所有单元格,但不能更新每个单元格。
欢迎任何想法或建议。
【问题讨论】:
标签: c# wpf mvvm datagrid strongly-typed-dataset