Пример #1
0
		public static async void BuildDataGridCellContent(DataGridCell cell, Func<CancellationToken, Task<FrameworkElement>> getContentFunction)
		{
			var originalContent = cell.Content;

			using (var cancellationTokenSource = new CancellationTokenSource())
			{
				FrameworkElement element;

				KeyEventHandler keyDownHandler = (sender, args) => cancellationTokenSource.CancelOnEscape(args.Key);

				try
				{
					cell.KeyDown += keyDownHandler;
					cell.Content = CreateTextBlock("Loading... ");
					element = await getContentFunction(cancellationTokenSource.Token);
				}
				catch (OperationCanceledException)
				{
					cell.Content = originalContent;
					return;
				}
				catch (Exception exception)
				{
					var textBlock = CreateTextBlock(exception.Message);
					textBlock.Background = Brushes.Red;
					element = textBlock;
				}
				finally
				{
					cell.KeyDown -= keyDownHandler;
				}

				cell.Content = ConfigureAndWrapUsingScrollViewerIfNeeded(cell, originalContent, element);
			}
		}