2017-11-13 14 views
1

TextBoxを含むDataGridTemplateColumnに2つのプロパティをバインドします。DataGridTemplateColumnに2つのプロパティをバインドします。TextBox WPF MVVM

私にはHSTという列があります。その列では、ユーザーが数式を入力して、フォーカスが残っているか、列が編集状態になっていない場合は、値が表示されます.MSと同様の動作です。

私は数式を格納している場所と、格納されている式の値を持つ場所が2つあります。

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エクセル、と同様の振る舞いを持っているがあります編集は私が私のviewmodelsから継承観測可能なオブジェクトと呼ばれるクラスを持っている

enter image description here

完了です。

このクラスには、ビュー要素を更新する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が私のデータグリッド上での行動を得意としたいです。

私が意味することにより、

、私はミリ秒で表現/式

の評価を表示する列から数式を表示する別の列を持ってしたくない時に編集状態の列が式/数式を表示するエクセルビュー状態のときにその式の値を表示します。

+0

PropChangeイベントのコードはどこにありますか? –

+0

私はそのためのカスタムコードを持っています。 inotifypropertychangedの子。そのRaisePropertyChangedと呼ばれる –

+0

あなたは何の質問ですか?私はこの投稿に質問はありません。私はあなたがMS Excelの動作を取得していないという問題を想定していますが、それは明示的には述べられていません。さらに、そのカスタムコードがなければ、私たちはあなたが何をしようとしているのか分からず、したがって問題が何であるか分かりません。提起されたイベントのコードを含めてください。 –

答えて

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> 
関連する問題