views:

264

answers:

2

Assuming I change the contents of a control using a XamlReader and add the UIElement to the container of a control, what events are supposed to fire? There are times where the SizeChanged will fire, LayoutUpdated changing.. though there are other times where neither of these occur despite having changing the contents of a control.

In my case, I am generating a thumbnail view of what's currently in view on a page. The user can change the content of the page and thus the thumbnail should update accordingly. Though, wiring to the LayoutUpdated, Loaded, SizeChanged aren't always reliable for when the contents have changed.

I would just call my InvalidateThumbnail which uses a writeablebitmap, but it's too quick after setting the content and as a result I will get a blank thumbnail.

At the moment, my hack (cringes) was to wait a few milliseconds before the UI is done rendering the actual new content and I can reliably create the thumbnail. I'd rather just trigger on an event every time though.

Possible? What events should I look at? I've seen CompositeTarget.Rendering but that's not what I want.

+1  A: 

Since content is a dependency property, you can use two way databinding and handle when the bound property changes. Here is an example

XAML

<Grid x:Name="LayoutRoot">
    <StackPanel>
        <ContentControl x:Name="ContentControl" Content="{Binding ContentProperty, Mode=TwoWay}"/>
        <Button Click="Button_Click" Content="Change Content"/>
    </StackPanel>
</Grid>

Code Behind

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        ContentControl.DataContext = new SomeObject();

    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ContentControl.Content = XamlReader.Load("<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Text=\"Hello\"/>");
    }
}

public class SomeObject
{
    private object _contentProperty = null;
    public object ContentProperty
    {
        get
        {
            return _contentProperty;
        }
        set
        {
            _contentProperty = value;
            MessageBox.Show("Content Changed");
        }
    }
}
Jacob Adams
What's weird is I can attach to the Content property of a grid to look for changes and invaldiate my thumbnail, but it doesn't fire every time despite having changed the content. I used this method but for some odd reason it doesn't work as expected.http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html
nyxtom
I'll use this as an alternative to the Grid control.
nyxtom
It still doesn't solve the issue of the thumbnail but the problem has to do with the way Silverlight's latent loaded method works.
nyxtom
My main issue is that it's too quick, I know that my xaml changed is firing and all events are firing to insert new content, it's the fact that I want to generate a thumbnail and the content hasn't yet rendered. As a result I have to set a hack to do it. Now that I know what the control lifecycle is, I can rely on that.
nyxtom
A: 

Although the events are occurring on their own I can use the ContentControl and use TwoWay databinding to ensure that when xaml is updated, I can insert the new xaml so that should work.

On the other half of my question, I did find out some of the issues surrounding when the visualtree is available and what the control lifecycle refers to.

http://blogs.msdn.com/devdave/archive/2008/10/11/control-lifecycle.aspx [http://blogs.msdn.com/silverlight_sdk/archive/2008/10/24/loaded-event-timing-in-silverlight.aspx]

nyxtom