1

I am capturing a visual representation of a WPF Window containing arbitrary content (controls, etc). I need to capture a picture of the Window each time the Window visually changes.

What can I do to tell when the Window has changed visually (size changed, content changed...anything the user can see with his eyes)? I don't care about any other changes.

I found some questions related to knowing when rendering is finished, but I'm not sure that's the exact same thing since I'm more concerned about the Window as a region, and I'm not concerned about the render happening if nothing visual changed.

3
  • Do you own the code of the application? Just handle the events that could cause a change. Apr 24, 2014 at 0:39
  • Narrow down the properties that participate on the content change and capture once they change plus capture once SizeChanged is called. In case you use MVVM you can leverage INotifyPropertyChanged and IChangeTracking interfaces in your viewmodels.
    – Peter
    Apr 24, 2014 at 7:18
  • Thanks for the suggestions. We do own the code of the application, but I was hoping not to need to walk down the trees and attach to events/properties. We get lots of notifications this way, which then need to be throttled. I was hoping for a more elegant solution if one exists. Apr 25, 2014 at 16:24

1 Answer 1

0

You can use the Window.LayoutUpdated event, it should give you what you need.
Consider the following:

<Window x:Class="WpfApplication11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        LayoutUpdated="MainWindow_OnLayoutUpdated">
    <StackPanel>
        <Label>Last event:</Label>
        <TextBox x:Name="LastEvent"/>
        <Label x:Name="Updateable">Updateable</Label>
        <UniformGrid x:Name="Controls"/>
    </StackPanel>
</Window>

and the code behind:

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();

        button_Click(null, null);

        var timer = new Timer(1000);
        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }

    private void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Dispatcher.Invoke(new Action(() =>
            {
                var rand = new Random();

                Updateable.Background =
                    new SolidColorBrush(Color.FromRgb((byte) rand.Next(byte.MaxValue),
                                                      (byte) rand.Next(byte.MaxValue),
                                                      (byte) rand.Next(byte.MaxValue)));
            }));
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        var button = new Button {Content = "Click Me!"};
        button.Click += button_Click;
        Controls.Children.Add(button);
    }

    private void MainWindow_OnLayoutUpdated(object sender, EventArgs eventArgs)
    {
        LastEvent.Text = DateTime.Now.ToLongTimeString();
    }
}
2
  • At least in my app, the ContentRendered event is only called once after the Window loads. If I resize it or move it, I don't see the event being called. Apr 30, 2014 at 17:38
  • @chrishibler - Oops, sorry, I meant the LayoutUpdated event. That should work.
    – CKII
    May 1, 2014 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.