将两个属性绑定到DataGridTemplateColumn文本框WPF MVVM

我想将两个属性绑定到包含TextBox的DataGridTemplateColumn。

我有一个名为HST的专栏。 在该列中,我希望用户input一个公式,当焦点离开时,或列不再处于编辑状态时,将显示该值,与MS Excel类似。

我有两个属性,我有公式存储以及我有公式存储的值。

public String SubTotal { get { String[] l_ComputeArr = l_HST.Split('='); if (l_ComputeArr.Length > 1) { DataTable dt = new DataTable(); try { var v = dt.Compute(l_ComputeArr[1], ""); l_SubTotal = v.ToString(); } catch { } } return l_SubTotal; } set { if (l_SubTotal != value) { l_SubTotal = value; } RaisePropertyChanged("SubTotal"); } } public String HST { get { return l_HST; } set { if (l_HST != value) { l_HST = value; } RaisePropertyChanged("HST"); RaisePropertyChanged("SubTotal"); } } 

小计有价值,HST有公式

我想隐藏HST并且与MS Excel具有相似的行为,在编辑小计时显示公式,并且在编辑完成后显示值

在这里输入图像说明

我有一个叫做observable对象的类,我的viewmodelinheritance自。

这个类有一个方法RaisePropertyChanged,它更新视图元素。

  public abstract class ObservableObject: INotifyPropertyChanged { [field: NonSerialized] public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { var handler = this.PropertyChanged; if (handler != null) { handler(this, e); } } protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpresssion) { var propertyName = PropertySupport.ExtractPropertyName(propertyExpresssion); this.RaisePropertyChanged(propertyName); } protected void RaisePropertyChanged(String propertyName) { VerifyPropertyName(propertyName); OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } /// <summary> /// Warns the developer if this Object does not have a public property with /// the specified name. This method does not exist in a Release build. /// </summary> [Conditional("DEBUG")] [DebuggerStepThrough] public void VerifyPropertyName(String propertyName) { // verify that the property name matches a real, // public, instance property on this Object. if (TypeDescriptor.GetProperties(this)[propertyName] == null) { Debug.Fail("Invalid property name: " + propertyName); } } } 

我的问题:

我想在我的数据网格上有类似的MS Excel的行为。

我的意思是,我不希望在显示expression式/公式的列中显示公式的单独列

在ms excel中,列处于编辑状态时显示expression式/公式,并在处于查看状态时显示该expression式的值。

 <DataGrid.Columns> <DataGridTemplateColumn> <!-- Template used when cell is in editing state. HST appears to be your formula. --> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBox Text="{Binding HST}" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> <!-- Template used when cell is not in editing state. --> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Subtotal}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns>