WPF-MVVM,Master - 使用计算列详细说明datagrid。费率*数量=总计

时间:2016-01-24 19:04:40

标签: c# wpf mvvm datagrid inline-editing

我有绑定到viewmodel的数据输入表单。我遵循EF代码优先方法,细节表由我的模型中的可观察集合表示,如下所示。

public partial class item
{ 
    public item()
    {
        orders = new ObservableCollection<order>();
    }
}

一切正常但在极少数情况下我有一个带有网格形式细节的主条目。这是xaml部分,它将SelectedItem.Orders绑定到细节网格。

<UserControl x:Class="ABCD.Views.itemView"
    ....
    DataContext="{Binding itemMaster, Source={StaticResource Locator}}"
    Height="Auto" Width="Auto">
    <Grid> 
        <Grid.ColumnDefinitions.... />
        <Grid.RowDefinitions..../>
        <DataGrid AutoGenerateColumns="False" VerticalAlignment="Top" ItemsSource="{Binding items}" SelectedItem="{Binding SelectedItem}" Name="dgitems" HorizontalAlignment="Stretch" RowDetailsVisibilityMode="VisibleWhenSelected" >
             <DataGrid.Columns>
                        <DataGridTextColumn x:Name="ItemNameColumn" Binding="{Binding Path=ItemName}" Header="Item Name" Width="125" />
                        <DataGridTextColumn x:Name="ItemCodeColumn" Binding="{Binding Path=ItemCode}" Header="Item Code" Width="75" />
                        <DataGridTextColumn x:Name="StockColumn" Binding="{Binding Path=StockLevel}" Header="Stock" Width="60" />
             </DataGrid.Columns>
        </DataGrid>
        <Label Grid.Column="3" Grid.Row="0" Margin="3,3,3,3" Content="Item Code"      VerticalAlignment="Center" />
        <TextBox Grid.Column="4" Grid.Row="0" HorizontalAlignment="Left" Margin="3,3,3,3" Name="ctrlItemCode" Text="{Binding Path=SelectedItem.ItemCode, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Width="120" Height="Auto" VerticalContentAlignment="Center" />
        <Label Grid.Column="3" Grid.Row="2" Margin="3,3,3,3" Content="Product Name" VerticalAlignment="Center" />
        <TextBox Grid.Column="4" Grid.Row="2" HorizontalAlignment="Left" Margin="3,3,3,3" Name="ctrlProductName" Text="{Binding Path=SelectedItem.ItemName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Width="120" Height="Auto" VerticalContentAlignment="Center" />
        <DataGrid Grid.Column="2" Grid.Row="16" Grid.ColumnSpan="3"  Height="145" AutoGenerateColumns="False" VerticalAlignment="Top" ItemsSource="{Binding SelectedItem.Orders}"  Name="dgOrders" HorizontalAlignment="Stretch" >
             <DataGrid.Columns>
                  <DataGridTemplateColumn Width="120">
                       <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                 <ComboBox DisplayMemberPath="OrderNumber" SelectedValuePath="OrderId"  ItemsSource="{Binding Path=DataContext.Orders,   RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" SelectedValue="{Binding Path=OrderId}" />
                             </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                  </DataGridTemplateColumn>
                  <DataGridTextColumn x:Name="QtyColumn" Binding="{Binding Path=Quantity}" Header="Qty" Width="75" />
                  <DataGridTextColumn x:Name="PriceColumn" Binding="{Binding Path=Price}" Header="Price" Width="75" />
                  <DataGridTextColumn x:Name="TotalColumn" Binding="{Binding Path=Total}" Header="Total" Width="75" />
              </DataGrid.Columns>
        </DataGrid>
   </Grid>
 </UserControl>

在这种情况下,我想在用户在网格中键入价格和数量时立即计算总数。我知道如果我们在viewmodel中有相关属性就可以处理它。但就我而言,Orders是项目模型中的一个集合。

提前致谢。

1 个答案:

答案 0 :(得分:0)

确定, 在论坛中搜索并寻求支持后,我找到了适合我案例的解决方案。只需在此处发布,这对某人有用。

简要介绍对我有用的最终答案。 可以做的是用viewmodel包装每个实体。 因此,使用该集合的数据网格将itermssource绑定到OrderVM的observablecollection而不是Order(实体类型)。

然后,您可以在我的viewmodel中使用属性包装实体的属性,并在其中放置任何额外的业务逻辑,如计算字段,并在其中一个因素发生变化时提高计算字段的属性。

根据这个建议,我的代码中的以下更改帮助我解决了问题; OrderVM集合如下所示;

 public class OrderVM : baseViewModel
{
    public OrderVM()
    {   }
    private order _order;
    public order Order
    {
        get
        {
            return _order;
        }
        set
        {
            _order = value;
            NotifyPropertyChanged();
        }
    }
public decimal Percentage
    {
        get
        {
            return (decimal)_order.Percentage; //Without passing the selectedOrder.Percentage like this, It doesn't work
        }
        set
        {
            _order.Percentage = value;
            NotifyPropertyChanged();
            calculate();
        }
    }
    public decimal Qty
    {
        get
        {
            return (decimal)_order.Qty;
        }
        set
        {
            _order.Qty = value;
            NotifyPropertyChanged();
            calculate();
        }
    }
    public decimal Total
    {
        get
        {
            return (decimal)_order.Total;
        }
        set
        {
            _order.Total = value;
            NotifyPropertyChanged();
        }
    }
    private void calculate()
    {
        _order.Total = _order.Price * _order.Qty;
        NotifyPropertyChanged("Total");
    }
 }

itemVM(MainVM)现在具有以下三个重要属性, Orders = orderVM的ObservableCollection SelectedItem //选择要编辑的项目 SelectedOrder //选择编辑的订单

public class itemsVM : baseViewModel
{
    private item _selectedItem;
    private ObservableCollection<OrderVM> _orders;
    public ObservableCollection<OrderVM> Orders
    {
        get
        {
            if (SelectedItem != null)
            {
                _orders = new ObservableCollection<OrderVM>();
                foreach (Order ord in SelectedItem.Orders)
                {
                    _orders.Add(new OrderVM { Order = ord });
                }
                return _orders;
            }
            else
                return null;
        }
        set { _orders = value; }
    }

// SelectedItem属性         公共项目SelectedItem         {             得到             {                 return _selectedItem;             }             组             {                 _selectedItem = value;                 NotifyPropertyChanged();                 NotifyPropertyChanged( “CanModify”);                 NotifyPropertyChanged( “订单”);             }         }

private OrderVM _selectedorder;
    public OrderVM SelectedOrder
    {
        get
        {
            return _selectedorder;
        }
        set
        {
            _selectedorder = value;
            NotifyPropertyChanged();
        }
    }
}

我的xaml如下;

<DataGrid Grid.Column="2" Grid.Row="16" Grid.ColumnSpan="3"  Height="145" AutoGenerateColumns="False" VerticalAlignment="Top" 
      ItemsSource="{Binding Orders}" SelectedItem="{Binding SelectedOrder}" Name="dgOrders" HorizontalAlignment="Stretch" >
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="QtyColumn" Binding="{Binding Path=Qty}" Header="Qty" Width="75" />
        <DataGridTextColumn x:Name="PriceColumn" Binding="{Binding Path=Price}" Header="Price" Width="75" />
        <DataGridTextColumn x:Name="TotalColumn" Binding="{Binding Path=Total}" Header="Price" Width="75" />
    </DataGrid.Columns>
</DataGrid>

可以在Conversation at MSDN Forums

找到总对话