具有计算值 (MVVM) 的组合框



有人可以帮助我吗? 首先,我写一个简单的例子,让你最能理解我。

假设,我有这样的VM:

我的数据上下文:

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:

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>

我再说一遍,这只是一个例子。

我有什么问题: 在我公司的业务逻辑中,ABonus 只能有以下两个值之一:2%*薪水或 0%*薪水 B-奖金,反过来只能有两个值,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>

它看起来像这样: 在此处输入图像描述

当然,我的 VM 必须提高 OnPropertyChanged A 奖励和 B 奖励项目以进行重新计算。

并且在模型中必须插入组合框的"选定项目"? 例如,值必须是值 200。 以及从数据库加载时必须将所选项目更改为。 对不起,我的英语不好,希望你能理解我xD

附言在此组合框中,始终只能是两个项目,0% 和"x"% 如果您可以说我执行此功能的最佳且用户友好的方法,请告诉我。 谢谢大家

我不确定我是否理解您要查找的内容,但我认为您应该在输入过程中更改条目。 意味着当您更改"薪水"中的值时,您应该更新视图模型中"A-奖金"和"B-奖金"列表中的项目。

也许这里有我的应用程序的小例子。 这是一个带有净值和布鲁托值的价格,但我实际上只持有净值。

-当净值更改时,我只触发Brutto属性更改事件 -当 brutto 值更改时,我更改了 netto 数据模型值并仅触发 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());
}
}

最新更新