2017-06-09 16 views
0

誰か助けてもらえますか? まず、私はあなたが私を最もよく理解できるという簡単な例を書いています。計算値(MVVM)を持つコンボボックス

私はこのようなVMのを持っている、のは、言ってみましょう:

私のDataContext:

public class DocRow 
{ 
    public DocRow(Employee employee, double salary, double aBonus, double bBonus) 
    { 
     Employee = employee; 
     Salary = salary; 
     ABonus = aBonus; 
     BBonus = bBonus; 
    } 

    public Employee Employee { get; set; } 
    public double Salary { get; set; } 
    public double ABonus { get; set; } 
    public double BBonus { get; set; } 
} 

と私のビュー:

<Window x:Class="TestComboboxUserControl.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:TestComboboxUserControl" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.DataContext> 
    <local:MainWindowVM/> 
</Window.DataContext> 
<Grid> 
    <DataGrid 
     ItemsSource="{Binding DocumentRows}"> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn Header="Employee"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding EmployeeList}" 
            SelectedItem="{Binding Employee,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></ComboBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
      <DataGridTemplateColumn Header="Salary"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBox Text="{Binding Salary,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
      <DataGridTemplateColumn Header="A-Bonus"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding EmployeeList}" 
            SelectedItem="{Binding ABonus,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></ComboBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
      <DataGridTemplateColumn Header="B-Bonus"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding EmployeeList}" 
            SelectedItem="{Binding BBonus,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></ComboBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
     </DataGrid> 
</Grid> 

public class MainWindowVM 
{ 
    private List<DocRow> _rows; 

    public List<DocRow> DocumentRows 
    { 
     get 
     { 
      return _rows ?? (_rows = new List<DocRow>() 
      { 
       new DocRow(new Employee(1,"Employee1"),200,4,2), 
       new DocRow(new Employee(2,"Employee2"),400,8,0) 
      }); 
     } 
    } 
} 

マイVM行について

これを繰り返しますが、これは単なる例です。

何私が持っている問題:、ABonusは2つの値のうち1つだけ持つことができ、私の会社のタマンロジックで :2%*給与または0%*給与 B-ボーナスを、自分のターンにのみ2つの値を持つことができ、 0.5%*給与または0%*給与。私が欲しいもの

<DataGridTemplateColumn Header="B-Bonus"> 
<DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <cbb:ComboBoxCalculated SelectedItem="{Binding BBonus,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> 
      <ComboboxCalculated.PredefinedPercent> 
       <CbbItem Value = "0%"/> 
       <CbbItem Value = "2%"/> 
      </ComboboxCalculated.PredefinedPercent> 
     </cbb:ComboBoxCalculated> 
    </DataTemplate> 
</DataGridTemplateColumn.CellTemplate> 

そして、それはこのようになりますよう: 私は私がこのように使用することができますどのようなユーザーコントロール、作成したい enter image description here

と私のVMを、もちろん、OnPropertyChanged AボーナスとBボーナスアイテムを再計算する必要があります。

そして、モデルでは、コンボボックスの "selectedItem"を挿入する必要がありますか?例として値200でなければなりません。 データベースの選択項目からのロードをに変更する必要がある場合 私の悪い英語を申し訳ありません、あなたが私を理解できることを願っています。xD

P.S.このコンボボックスでは、私がこの機能を実行するのに最高のuserfriendly方法を言うことができる場合は、常に2つの項目、0%と "x"% することができます、私に話してください。 ありがとうございました

答えて

0

私はあなたが何を探しているのか分かりませんが、入力中にエントリを変更する必要があると思います。 'Salary'の値を変更すると、ビューモデル内のリスト 'A-Bonus'と 'B-Bonus'のアイテムを更新する必要があることを意味します。

ここに私のアプリケーションの少しの例があります。それはネットトとbruttoの値と価格ですが、私は本当にネットの値を保持しています。

-nettoの値が変更されたとき、私はちょうどbruttoプロパティの変更イベントをトリガーします - bruttoの値が変更されると、netto DATA MODEL値が変更され、nettoプロパティ変更イベントだけがトリガーされます。

/// <summary>price purchase netto</summary> 
    [DefaultValue(null)] 
    [JsonIgnore] 
    [Validation.RequiredIf("Class", ErrorMessageResourceType = typeof(CarPlus.Base.Properties.Resources), ErrorMessageResourceName = "General_Required")] 
    [Range(0, 10000000, ErrorMessageResourceType = typeof(CarPlus.Base.Properties.Resources), ErrorMessageResourceName = "ViewModelVehicle__PriceRange")] 
    public decimal? PricePurchaseNetto 
    { 
     get 
     { 
      return (this.__dataModel.PricePurchaseNetto); 
     } 
     set 
     { 
      if (this.__dataModel.PricePurchaseNetto != value) 
      { 
       this.__dataModel.PricePurchaseNetto = value; 
       this.RaisePropertyChanged("PricePurchaseNetto"); 
       Log.Logger.DebugFormat("{0}:{1} - set('{2}')", this.FullName, MethodBase.GetCurrentMethod().Name, value == null ? "NULL" : value.ToString()); 


       this.RaisePropertyChanged("PricePurchaseBrutto"); 
      } 
     } 
    } 

    //------------------------------------------------------------------------------------- 
    /// <summary>price purchase brutto</summary> 
    [DefaultValue(null)] 
    [JsonIgnore] 
    [Validation.RequiredIf("Class", ErrorMessageResourceType = typeof(CarPlus.Base.Properties.Resources), ErrorMessageResourceName = "General_Required")] 
    [Range(0, 10000000, ErrorMessageResourceType = typeof(CarPlus.Base.Properties.Resources), ErrorMessageResourceName = "ViewModelVehicle__PriceRange")] 
    public decimal? PricePurchaseBrutto 
    { 
     get 
     { 
      return (Helpers.GetBrutto(this.__dataModel.PricePurchaseNetto, this.VatVerifiable && !this.VatImportWithout ? this.VatPurchase : null)); 
     } 
     set 
     { 
      this.__dataModel.PricePurchaseNetto = Helpers.GetNetto(value, this.VatVerifiable && !this.VatImportWithout ? this.VatPurchase : null); 
      this.RaisePropertyChanged("PricePurchaseNetto"); 
      Log.Logger.DebugFormat("{0}:{1} - set('{2}')", this.FullName, MethodBase.GetCurrentMethod().Name, value == null ? "NULL" : value.ToString()); 
     } 
    } 
関連する問題