views:

62

answers:

2

Hi,

maybe you could help me understand why I get an unhandled exception "Invalid XAML" in Visual Studio 2010 designer when trying to do the following on a Page.

I have a Converter named DateTimeConverter that converts a date into a German date string. The converter works fine. I've included the namespace and added the following to the page's resources:

<navigation:Page.Resources>
    <myClasses:DateTimeConverter x:Key="dateTime" />
</navigation:Page.Resources>

Now I have a list box that I want to bind to a list of objects. I do the binding in code, but I would like to define the data template. Thus I've added the following to my layout:

<ListBox x:Name="lbConversation" BorderBrush="#00000000">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="0" Padding="4">
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Message, Mode=OneWay}" />
                    <TextBlock Text="{Binding TimeStamp, Mode=OneWay, Converter={StaticResource dateTime}}" />
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And this works fine when I run. However, in the code section, the code for the data template is undercurled and the designer says "Invalid XAML". When I remove the Converter=... part, this error is gone.

Is this a designer bug? What can I do?

EDIT
By the way: The exact same code does not throw the error within a WPF project!

A: 

Sorry, can't replicate this at all, do you have some design-time data that may be the cause of the weird error?

Also, since you said that you're using the converter to output german dates... wouldn't it be easier to let the framework do this kind of things, as it probably does them a lot better? Set the entire application thread's CultureInfo to german and all formatting will be done with that culture's settings; of course it's still possible you only want some controls internationalized...

Alex Paven
Hi, thanks for your reply. Of course I could handle the date formatting by framework. But I have other converters too, and the problem uccurs also for those converters.
Thorsten Dittmar
A: 

I can't see anything wrong with your Xaml however I wonder if this is a result of the language setting used when the Xaml is parsed. By default Xaml is parsed using the InvariantCulture however it would appear that the designer in visual studio parses the Xaml using the current culture. Hence at times you can get unexpected differences in behaviour at design time than you do at runtime.

In fact if you do this in the constructor of your UserControl before calling InitializeComponent:-

 this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

You might not need your converter at all.

AnthonyWJones