【问题标题】:Combobox with calculated value(MVVM)具有计算值的组合框(MVVM)
【发布时间】:2017-06-09 15:18:23
【问题描述】:

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

假设我有这样的虚拟机:

我的数据上下文:

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)
            });
        }
    }
}

我的行虚拟机:

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%*Salary 或 0%*Salary B-Bonus 轮到他只能有两个值,即 0.5%*Salary 或 0%*Salary。

我想要什么: 我想创建一个用户控件,我可以这样使用:

<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

当然,我的虚拟机必须提高 OnPropertyChanged A-Bonus 和 B-Bonus 项目以进行重新计算。

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

附:在这个组合框中,总是只能是两个项目,0% 和 "x"% 如果你能说我做这个功能的最好和用户友好的方式,请告诉我。 谢谢大家

【问题讨论】:

    标签: c# wpf mvvm combobox


    【解决方案1】:

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

    也许这里是我的应用程序的一个小例子。这是一个包含 netto 和 brutto 价值的价格,但我真的只持有 netto 价值。

    -当 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());
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多