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

C# 将两个属性绑定到DataGridTemplateColumn文本框WPF MVVM,c#,wpf,excel,mvvm,C#,Wpf,Excel,Mvvm,我想将两个属性绑定到包含文本框的DataGridTemplateColumn 我有一个专栏叫HST。在该列中,我希望用户输入公式,当焦点离开或该列不再处于编辑状态时,将显示该值,与MS excel的行为类似 我有两个属性,一个是存储公式,另一个是存储公式中的值 public String SubTotal { get { String[] l_ComputeArr = l_HST.Split('='); i

我想将两个属性绑定到包含文本框的DataGridTemplateColumn

我有一个专栏叫HST。在该列中,我希望用户输入公式,当焦点离开或该列不再处于编辑状态时,将显示该值,与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具有类似的行为,其中编辑小计时显示公式,编辑完成后显示值

我的viewmodels继承了一个名为observable object的类

此类有一个方法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);
           }
       }
   }
公共抽象类ObserveObject:INotifyPropertyChanged
{
[字段:非序列化]
公共事件属性更改事件处理程序属性更改;
PropertyChanged上受保护的虚拟无效(PropertyChangedEventArgs e)
{
var handler=this.PropertyChanged;
if(处理程序!=null)
{
处理者(本,e);
}
}
受保护的void RaisePropertyChanged(表达式属性Expression)
{
var propertyName=PropertySupport.ExtractPropertyName(PropertyExpression);
this.RaisePropertyChanged(propertyName);
}
受保护的void RaisePropertyChanged(字符串propertyName)
{
验证propertyName(propertyName);
OnPropertyChanged(新PropertyChangedEventArgs(propertyName));
}
/// 
///如果此对象没有具有的公共属性,则警告开发人员
///指定的名称。发布版本中不存在此方法。
/// 
[有条件的(“调试”)]
[调试步骤至]
public void VerifyPropertyName(字符串propertyName)
{
//验证属性名称是否与实数匹配,
//此对象的公共、实例属性。
if(TypeDescriptor.GetProperties(此)[propertyName]==null)
{
Debug.Fail(“无效的属性名称:“+propertyName”);
}
}
}
我的问题:

我希望在我的datagrid上有类似的ms excel行为

我的意思是,我不想让显示公式的列与显示表达式/公式求值的列分开

在ms excel中,一列在编辑状态下显示表达式/公式,在查看状态下显示该表达式的值。


<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>

PropChange事件的代码在哪里?我有一个自定义代码。一个InotifyProperty的孩子被改变了。它叫RaiseProperty Changed你在问什么问题?我在这篇文章中没有看到任何问题。我假设问题是您没有获得MS Excel行为,但这没有明确说明。此外,如果没有自定义代码,我们不知道您在尝试什么,因此也不知道问题出在哪里。请包含所引发事件的代码。@JacobBarnes我已编辑了我的问题并包含了代码。使我的问题更加清晰明了。非常感谢。