I have been trying to learn and take advantage of WPF and databinding. I have a listview that has a column that will display one of three images as shown in this snippet:
<GridViewColumn Header="Status" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image x:Name="TheImage" Height="18"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Status}" Value="queued">
<Setter TargetName="TheImage" Property="Source" Value="Images\que_48.png" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Status}" Value="completed">
<Setter TargetName="TheImage" Property="Source" Value="Images\complete_48.png" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Status}" Value="failed">
<Setter TargetName="TheImage" Property="Source" Value="Images\fail_48.png" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I have a class (BatchQueueItem) that has among it this code for handling the PropertyChange Event:
public string status;
public string Status
{
get { return status; }
set
{
status = value;
OnPropertyChanged("Status");
}
}
// Create the OnPropertyChanged method to raise the event
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string status)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(status));
}
}
And I have a button on the listview window page:
private void btnStart_Click(object sender, RoutedEventArgs e)
{
foreach (var item in listView1.Items)
{
BatchQueueItem bqi = (BatchQueueItem)item;
string currFile = bqi.CurrFile;
if (mainWindow.isIsbnInFileName(ref currFile))
{
bqi.Status = "completed";
}
else
{
bqi.Status = "failed";
}
}
}
The problem I am having is that the images do not update until after the foreach loop has completed and the btnStart_Click() method has completed. Once that happens, all the images update as expected, just all at once not iteratively.
What I am wanting, and what I thought would happen, was that each iteration of the foreach loop would update the corresponding row's image. Undoubtedly I am missing something important about how this all works. Any tips?