Tech Off Thread

2 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

XamlObjectWriter fails with bindings?

Back to Forum: Tech Off
  • User profile image
    EduFer

    Hi All.

    I'm Trying to load the following basic xaml with XamlObjectWriter (.Net 4)

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            Title="MainWindow" Height="350" Width="525" x:Name="mainWindow">
      <Grid>
            <TextBox Name="myTextBox" Height="23" HorizontalAlignment="Left" Margin="271,71,0,0" VerticalAlignment="Top" Width="120" />
            <Button Content="{Binding ElementName=myTextBox, Path=Text}" Width="100" Margin="97,4,306,0"/>
        </Grid>
    </Window>

     

     

     The code is quite simple:

                XmlReader xr = XmlReader.Create("../../XAMLSimpl.xaml");

                XamlXmlReader reader = new XamlXmlReader(xr);

                XamlObjectWriter writer = new XamlObjectWriter(reader.SchemaContext);

                while (reader.Read())
                {
                    writer.WriteNode(reader);
                }

                object res = writer.Result;

     When I run the the code I get the following exception (because the binding)

    {"A 'Binding' cannot be set on the 'Content' property of type 'Button'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject."}

    A 'Binding' cannot be set on the 'Content' property of type 'Button'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

     

    Which looks. If I try to use the old style code:

    FileStream reader = new FileStream("../../XAMLSimpl.xaml", FileMode.Open);
    object res = System.Windows.Markup.XamlReader.Load(reader);
     

     Everything works fine.

    Any suggestion?

     

    Many thanks in advance,

     

    Edu.

     

     

     

     

  • User profile image
    Rob Relyea

    see http://michaelshim.com/blog/2009/12/10/use-xamlreader-load-for-wpf-xaml-not-xamlservices-load for some details.

    It depends what you are trying to do with your XamlNode loop...

    You could use the Wpf SchemaContext + your code (pass it to the constructor of the XamlXmlReader), and you'll get pass this binding problem.  But there are other things in XamlReader.Load() that won't happen. See the blog post for the list.

    Thanks,
    Rob Relyea, WPF Team
    http://robrelyea.com/blog | http://twitter.com/rrelyea

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.