XamlReader + StaticResource data binding + RenderTargetBitmap problem

I have the following XAML which I got from samples:

<StackPanel>
<StackPanel.Resources>

<DataTemplate DataType="Hero">
<TextBlock Text="{Binding XPath=@Name}" Foreground="Cyan"/>
</DataTemplate>

<XmlDataProvider x:Key="GreekHeroesData" XPath="GreekHeroes/Hero">
<x:XData>
<GreekHeroes xmlns="">
<Hero Name="Jason" />
<Hero Name="Hercules" />
<Hero Name="Perseus" />
</GreekHeroes>
</x:XData>
</XmlDataProvider>

</StackPanel.Resources>

<TextBlock FontSize="18" FontWeight="Bold" Margin="10"
HorizontalAlignment="Center">Composite Collections Sample</TextBlock>

<ListBox Name="lbGodsAndHeroes" Height="300" Width="200" Background="White"
ListBox.ItemsSource="{Binding Source={StaticResource GreekHeroesData}}"/>
</StackPanel>

It works fine, If I run it as XAML Windows Application, i.e. I'm able to see list box items (in this case I put it into <Window>).

If I read it using XamlReader and render to image by RenderTargetBitmap then ListBox items will not appear on the image (data binding doesn't work).

By analogy with ASP.NET I thought that it should be something like DataBind() function. I tryed to use BindingExpression.UpdateSource() - but this doesn't help.

What could be a problem How to force data binding in WPF

Regards, Andrey




Answer this question

XamlReader + StaticResource data binding + RenderTargetBitmap problem

  • frank stone

    DataBinding does not happy synchronously.

    Thread.Sleep(3000) won’t help (on the UI thread).

    You should cue up a dummy method using Dispatcher.Invoke with a low priority (use Background). In this dummy method, try to access the image.



  • Mika Joronen

    Found walkaround:

    Adding IsAsynchronous="False" to <XmlDataProvider> solves the issue.

    Still having issues when IsAsynchronous="True". Thread.Sleep(30000) after XamlReader didn't help.

    Any Ideas



  • XamlReader + StaticResource data binding + RenderTargetBitmap problem