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开发者_Python百科 that's not what I want.
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");
}
}
}
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]
精彩评论