i have a query like this, and it throws me an error, which says imgSortUp does not exist in the current context. Is this a bug? Any ideas? It was just working fine before I put the declaration of images inside the if statement. Any help would be appreciated.
ContentPresenter cp = (sender as Grid).GetVisualDescendants().OfType<ContentPresenter>().SingleOrDefault();
if (cp.Content.ToString() == "Work Order #" || cp.Content.ToString() == "Status")
{
Image imgSortUp = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault();
Image imgSortDown = (sender as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault();
}
else
return;
if (clearOldSortIcons != null)
{
Image oldSortIconUp = (clearOldSortIcons as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconUp").SingleOrDefault();
Image oldSortIconDown = (clearOldSortIcons as Grid).GetVisualDescendants().OfType<Image>().Where(i => i.Name == "SortIconDown").SingleOrDefault();
oldSortIconUp.Visibility = System.Windows.Visibility.Collapsed;
oldSortIconDown.Visibility = System.Windows.Visibility.Collapsed;
}
start:
if (!string.IsNullOrEmpty(cp.Content.ToString()) && (string.IsNullOrEmpty(sortCheck) || sortCheck == cp.Content.ToString()))
{
if (hasSorted == false)
{
switch (cp.Content.ToString())
{
case "Work Order #":
imgSortUp.Visibility = System.Windows.Visibility.Visible;
break;
case "Status":
imgSortUp.Visibility = System.Windows.Visibility.Visible;
break;
}
hasSorted = true;
}
else
{
switch (cp.Content.ToString())
{
case "Title":
imgSortDown.Visibility = System.Windows.Visibility.Visible;
break;
case "Status":
imgSortDown.Visibility = System.Windows.Visibility.Visible;
break;
}
hasSorted = false;
}
}
else
{
sortCheck = cp.Content.ToString();
hasSorted = false;
goto start;
}
sortCheck = cp.Content.ToString();
clearOldSortIcons = sender;
SortItems(cp.Content.ToString(), hasSorted);
dpVideos.Source = grdVideos.ItemsSource;
This is because you declare the images within their own scope and use them later: