I have DataGrid binded to object with information about every column (using Caliburn Micro Framework). Based on values of some columns, when I click specific button data in other columns are calculated. But to see changes I have to either click to sort any column or to scroll down and up. So, I'm shure data is calculated correct and values are changed. But I would want to see changes when they occur (after clicking the button). Don't know how to "refresh" what is seen on the screen. I found some answers but they don't satisfy me. Most of them assume that I have DataGrid object in ViewModel and I have something different. Is it possible to refresh view after change?
Selected item in DataGrid class:
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
In my ShellViewModel I have object of that class named SelectedItem.
Button code:
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>
EDIT:
Selected Item in ViewModel
public ItemModel SelectedItem
{
get { return selectedItem; }
set {
selectedItem = value;
NotifyOfPropertyChange(() => SelectedItem);
}
} // SelectedItem
Adding "NotifyOfPropertyChange(() => SelectedItem);" didn't change anything.
It looks like your ItemModel lacks an implementation of INotifyProperyChanged interface so the framework doesn't know that something has changed.