如何在MVVM模式中添加行?

时间:2016-09-01 00:50:43

标签: xaml silverlight mvvm telerik

假设我使用xaml创建telerik RadGridView。

<telerik:RadGridView x:Name="myRadGridView" 
                             Width="1000" 
                             IsReadOnly="True" 
                             ValidatesOnDataErrors="None" 
                             AutoGenerateColumns="False" 
                             IsFilteringAllowed="False" 
                             ShowGroupPanel="False" 
                             ShowColumnFooters="False" 
                             CanUserResizeColumns="False"
                             CanUserFreezeColumns="False" 
                             SelectionMode="Single" 
                             CanUserReorderColumns="False" 
                             CanUserSortColumns="False" 
                             CanUserInsertRows="False" 
                             CanUserDeleteRows="False"
                             CanUserSelect="True" 
                             RowIndicatorVisibility="Visible" 
                             Height="250" 
                                ScrollViewer.VerticalScrollBarVisibility="Visible"
                             ItemsSource="{Binding Information}"
                             SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                             SelectionChanged="myRadGridView_SelectionChanged"
            >
            <telerik:RadGridView.Columns>
            <telerik:GridViewColumn />
            <telerik:GridViewColumn />
            .....
            </telerik:RadGridView.Columns>

有很多专栏。 我从ViewModel获取数据。现在我想添加一个新行来获取上述行的总值。

我的问题是如何在我的ViewModel中添加行?

1 个答案:

答案 0 :(得分:0)

试试这个:

有了这个,您可以通过XAML(不需要其他代码)

为特定列执行此操作
    <telerik:GridViewDataColumn Header="Quantity"
                            DataMemberBinding="{Binding ProdQuantity}"
                            UniqueName="Quantity">
     <telerik:GridViewDataColumn.AggregateFunctions>
         <telerik:SumFunction Caption="Sum: " />
     </telerik:GridViewDataColumn.AggregateFunctions>
</telerik:GridViewDataColumn>

对于所有数字列

<telerik:RadGridView x:Name="myRadGridView" 
                     Width="1000" 
                     IsReadOnly="True" 
                     ...            

                     ShowColumnFooters="True" 
                     AutoGeneratingColumn="GridView_AutoGeneratingColumn"

                     ItemsSource="{Binding Information}"
                     SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                     SelectionChanged="myRadGridView_SelectionChanged"
                     > 

在代码中:

    private void GridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
    {
        if(!e.Column is GridViewDataColumn)
            return;
        GridViewDataColumn col = (e.Column as GridViewDataColumn);
        if (col.DataType != null)
        {
            if (col.DataType == typeof(Int32)  || col.DataType == typeof(long) || col.DataType == typeof(Double))           
                 e.Column.AggregateFunctions.Add(new SumFunction());            
        }   
    }

请注意,列必须是GridViewDataColumn