简体   繁体   中英

Cannot place a resource inside a ContentControl

This XAML does not work in Windows Store apps:

<ContentControl Grid.Column="1" Content="{StaticResource AddUserStep1Control}" />

Where the user control is loaded as a resource in the App.xaml like so:

<featuresUsers:BoyGirlPickerUserControl x:Key="AddUserStep1Control" />

And the control looks like this:

<UserControl>

.. namespace etc.

    <Grid>
        <Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="10" />
                <Setter Property="FontSize" Value="96" />
                <Setter Property="Padding" Value="50, 20" />
            </Style>
        </Grid.Resources>
        <StackPanel>
            <TextBlock HorizontalAlignment="Center" FontSize="96" Margin="0,50,0,30">I am a...</TextBlock>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Button>Boy</Button>
                <Button>Girl</Button>
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl>

As I understand it, a ContentControl can contain anything. I was binding the Content property to the view-model and then getting the UserControl from the application resources and setting it on the view-model property but that gave another error which led me to try doing it all in XAML.

I was able to instantiate a TextBlock in code in the view-model and set it on the bound property and see that control and text in the UI, but not so with the UserControl .

Here's the error message:

A first chance exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in GlyphWriterStoreApp.exe

WinRT information: Failed to assign to property 'Windows.UI.Xaml.Controls.ContentControl.Content' because the type 'Windows.UI.Xaml.Controls.UserControl' cannot be assigned to the type 'Object'. [Line: 55 Position: 45]

Additional information: The text associated with this error code could not be found.

Failed to assign to property 'Windows.UI.Xaml.Controls.ContentControl.Content' because the type 'Windows.UI.Xaml.Controls.UserControl' cannot be assigned to the type 'Object'. [Line: 55 Position: 45]

If there is a handler for this exception, the program may be safely continued.

Update 1

So the problem is setting controls that are resources as the Content . I just tried with a TextBlock that is declared in App.xaml and that gives the same error.

Seems to be a known issue.

http://social.msdn.microsoft.com/Forums/silverlight/en-US/9076af75-07e2-40b4-8890-6ea546d1e9de/cant-use-staticresource-as-content-of-contentcontrol

That's odd because even loading the resource in code, which yields an instance of whatever control, doesn't work, whereas instantiating the control in code does work even though they're the same.

They're just object references. Very odd. Looks like I'll have to dynamically instantiate my control types at runtime or use a template selector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM