TaskbarItemInfo.Overlay binding to a DrawingImage instance stops image updates by code

341 Views Asked by At

I'm trying to use a GeometryDrawing with a dynamic content as source for TaskbarItemInfo.Overlay. The image in code below is based on a GeometryDrawing which Brush is continuously updated by code.

1/ For test purpose, a copy of the image with Source bound to the original image Source works as expected, the two images are updated on the screen.

2/ Adding to XAML:

<Window.TaskbarItemInfo>
    <TaskbarItemInfo Overlay="{Binding ElementName=image, Path=Source}"/>
</Window.TaskbarItemInfo>

This is the same binding to have the same image as a task bar icon overlay. This stops updates for everything.

I'm learning WPF and don't understand why this happens.

XAML:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Test"
    mc:Ignorable="d"

    Title="MainWindow" Width="400" Height="200">

<StackPanel Orientation="Horizontal">

    <Image x:Name="image" Width="100" Stretch="Uniform" Margin="10,10,5,10">
        <Image.Source>
            <DrawingImage>
                <DrawingImage.Drawing>
                    <GeometryDrawing >
                        <GeometryDrawing.Geometry>
                            <RectangleGeometry Rect="0, 0, 10, 10"/>
                        </GeometryDrawing.Geometry>
                        <GeometryDrawing.Brush>
                            <VisualBrush>
                                <VisualBrush.Visual>
                                    <StackPanel>
                                        <TextBlock Width="10" HorizontalAlignment="Center" VerticalAlignment="Center"
                                                   Background="Red" Foreground="White"
                                                   FontSize="10" FontWeight="Normal"
                                                   Text="{Binding Path=TextOnImage}"/>
                                    </StackPanel>
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </GeometryDrawing.Brush>
                    </GeometryDrawing>
                </DrawingImage.Drawing>
            </DrawingImage>
        </Image.Source>
    </Image>

    <Image Width="100" Stretch="Uniform" Margin="5,10,10,10"
           Source="{Binding ElementName=image, Path=Source}"/>
</StackPanel>

C#:

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;

namespace Test {
    public partial class MainWindow : Window {
        ViewModel viewModel = new ViewModel ();

        public MainWindow () {
            InitializeComponent ();
            DataContext = viewModel;
        }
    }

    public class ViewModel : INotifyPropertyChanged {
        string textOnImage;
        public event PropertyChangedEventHandler PropertyChanged;
        public string TextOnImage { get => textOnImage; set { textOnImage = value; OnPropertyChange (); } }

        public ViewModel () {
            var dispatcherTimer = new System.Windows.Threading.DispatcherTimer () {
                dispatcherTimer.Interval = new TimeSpan (0, 0, 1);
            }
            dispatcherTimer.Tick += new EventHandler ((sender, e) => TextOnImage = (TextOnImage == "X" ? "O" : "X"));
            dispatcherTimer.Start ();
        }

        void OnPropertyChange ([CallerMemberName] string property = null) {
            PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (property));
        }
    }
}

Can you please point me in the right direction to understand and fix the issue?

Edit: I discovered another problem with the same code, which is perhaps linked.

When TaskbarItemInfo.Overlay is bound to the image, and the window size is reduced to the point the image size is itself reduced, then the text appears in image, and also as overlay over the taskbar icon, i.e. it works correctly. However after several size adjustments an exception occurs:

System.InvalidOperationException' occurred in mscorlib.dll Collection was modified; enumeration operation may not execute

I assume this is a problem of accessing a collection from different threads, but this doesn't really help me understanding the issue.

0

There are 0 best solutions below