将两个属性绑定到DataGridTemplateColumn TextBox WPF MVVM

时间:2017-11-13 18:37:29

标签: c# wpf excel mvvm

我想将两个属性绑定到包含TextBox的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有类似的行为,其中在编辑小计时显示公式,并在编辑完成后显示值

enter image description here

我有一个名为observable对象的类,我的viewmodels继承自。

此类有一个方法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行为。

我的意思是,我不想在显示表达式/公式评估的列中显示单独的列

在ms excel中,列处于编辑状态时显示表达式/公式,并在处于视图状态时显示该表达式的值。

1 个答案:

答案 0 :(得分:1)

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