列值更改后刷新数据网格

时间:2019-02-13 16:11:17

标签: c# wpf mvvm datagrid caliburn

我已将DataGrid绑定到具有有关每列信息的对象(使用Caliburn Micro Framework)。基于某些列的值,当我单击特定按钮时,将计算其他列中的数据。但是要查看更改,我必须单击以对任何列进行排序或上下滚动。因此,我确定数据计算正确且值已更改。但是,我想看到它们发生时的更改(单击按钮后)。不知道如何“刷新”屏幕上看到的内容。我找到了一些答案,但他们不满意我。他们中的大多数人都假定我在ViewModel中有DataGrid对象,而我有一些不同之处。更改后是否可以刷新视图?

DataGrid类中的选定项目:

public class ItemModel
{
    private string code;

    public string Code
    {
        get { return code; }
        set { code = value; }
    }

    private int targetCover = 10;

    public int TargetCover
    {
        get { return targetCover; }
        set { targetCover = value; }
    } // TargetCover

    private bool week_1 = true;

    public bool Week_1
    {
        get { return week_1; }
        set { week_1 = value;
            Calculate_avg_sales();
        }
    } // Week_1

    private bool week_2 = true;

    public bool Week_2
    {
        get { return week_2; }
        set { week_2 = value;
            Calculate_avg_sales();
        }
    } // Week_2

    private bool week_3 = true;

    public bool Week_3
    {
        get { return week_3; }
        set { week_3 = value;
            Calculate_avg_sales();
        }
    } // Week_3

    private bool week_4 = true;

    public bool Week_4
    {
        get { return week_4; }
        set { week_4 = value;
            Calculate_avg_sales();
        }
    } // Week_4
    private bool week_5 = true;

    public bool Week_5
    {
        get { return week_5; }
        set { week_5 = value;
            Calculate_avg_sales();
        }
    } // Week_5

    private BindableCollection<ItemStoresModel> stores = new BindableCollection<ItemStoresModel>();

    public BindableCollection<ItemStoresModel> Stores
    {
        get { return stores; }
        set { stores = value; }
    }

    public ItemModel()
    {

    } // ItemModel()

    public void Allocation()
    {
        Clear_allocation();

        foreach (var item in Stores)
        {
            if (item.Eff_aft_all < item.Min)
            {
                while(item.Eff_aft_all < item.Min)
                {
                    item.Pc_all += 1;
                    item.Calculate_after_allocation();
                }
            } // if eff_stock is less than minimum

            if (item.Af_all_cov < TargetCover && 
                item.Eff_aft_all < item.Max &&
               (item.Eff_aft_all + item.Qty_multiple) <= item.Max)
            {
                while(item.Af_all_cov < TargetCover &&
                      item.Eff_aft_all < item.Max &&
                     (item.Eff_aft_all + item.Qty_multiple) <= item.Max)
                {
                    item.Pc_all += 1;
                    item.Calculate_after_allocation();
                } // while conditions are meet, allocate additional pack
            } // if cover after allocation is less than targeted cover                
        } // foreach() item (store) calcuate allocation            
    } // Allocation()

    public void Clear_allocation()
    {
        foreach (var item in Stores)
        {
            item.Pc_all = 0;
            item.Calculate_after_allocation();
        }
    } // Clear_allocation()


    public void Calculate_avg_sales()
    {
        int number;
        int result;

        foreach (var item in Stores)
        {
            number = 0;
            result = 0;

            if (Week_1) { number++; result += item.Week_1; }
            if (Week_2) { number++; result += item.Week_2; }
            if (Week_3) { number++; result += item.Week_3; }
            if (Week_4) { number++; result += item.Week_4; }
            if (Week_5) { number++; result += item.Week_5; }
            if (number==0) { item.Avg_sales = 0; }
            if (number!=0) { item.Avg_sales = (double)(result) / number;}
        } // foreach   

    } //Calculate_avg_sales()

} // class ItemModel

在ShellViewModel中,我有一个名为SelectedItem的类的对象。

按钮代码:

public void Allocate()
    {
        SelectedItem.Allocation();

    } // Allocate()

XAML:

 <DataGrid x:Name="stores" SelectionUnit="CellOrRowHeader"  AutoGenerateColumns="False" 
                              CanUserAddRows="False" SelectionMode="Single" ItemsSource="{Binding SelectedItem.Stores}">

                        <DataGrid.Resources>
                            <SolidColorBrush x:Key="backColour" Color="#E9E9E9" />
                        </DataGrid.Resources>

                        <DataGrid.Columns>

                            <DataGridTextColumn Header="Str" Binding="{Binding Str}">                                    
                                <DataGridTextColumn.CellStyle>
                                    <Style TargetType="DataGridCell">
                                        <Setter Property="Background" Value="{StaticResource backColour}"/>
                                        <Setter Property="Foreground" Value="Black" />
                                    </Style>
                                </DataGridTextColumn.CellStyle>
                            </DataGridTextColumn>

                            <DataGridTextColumn Header="Grade" Binding="{Binding Grade}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Avg sales" FontWeight="SemiBold" Binding="{Binding Avg_sales, StringFormat=F1}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Week 1" Binding="{Binding Week_1}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Week 2" Binding="{Binding Week_2}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Week 3" Binding="{Binding Week_3}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Week 4" Binding="{Binding Week_4}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Week 5" Binding="{Binding Week_5}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Cover" FontWeight="SemiBold" Binding="{Binding Cover, StringFormat=F1}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Af. all cov" FontWeight="SemiBold" Binding="{Binding Af_all_cov, StringFormat=F1}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Min" Binding="{Binding Min, Mode=TwoWay}" IsReadOnly="False"/>
                            <DataGridTextColumn Header="Max" Binding="{Binding Max, Mode=TwoWay}" IsReadOnly="False"/>
                            <DataGridTextColumn Header="Stock" Binding="{Binding Stock}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Eff stock" FontWeight="SemiBold" Binding="{Binding Eff_stock}"  IsReadOnly="True"/>
                            <DataGridTextColumn Header="Eff aft. all" FontWeight="SemiBold" Binding="{Binding Eff_aft_all}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Qty all" Binding="{Binding Qty_all}" IsReadOnly="True"/>
                            <DataGridTextColumn Header="Pc. all" FontWeight="Bold" Binding="{Binding Pc_all , Mode=TwoWay}" IsReadOnly="True"/>
                        </DataGrid.Columns>
                    </DataGrid>

编辑:

在ViewModel中选择的项

public ItemModel SelectedItem
    {
        get { return selectedItem; }
        set {
            selectedItem = value;
            NotifyOfPropertyChange(() => SelectedItem);
            }
    } // SelectedItem

添加“ NotifyOfPropertyChange(()=> SelectedItem);”没有任何改变。

1 个答案:

答案 0 :(得分:0)

您的ItemModel似乎缺少INotifyProperyChanged接口的实现,因此框架不知道某些更改。