Binding Content Content Control Properties in WinRT

Say I have an application for the Windows Store (targeting Windows 8.1), and the page has a ContentControl that looks like this:

<ContentControl> <ContentControl.Content> <TextBlock>Hello world</TextBlock> </ContentControl.Content> </ContentControl> 

This works absolutely fine, but if I try to set the content as a resource, for example:

 <Page.Resources> <TextBlock x:Key="TestContent">Hello world</TextBlock> </Page.Resources> <ContentControl Content="{StaticResource TestContent}" /> 

Everything looks great in the designer, but at runtime I get the following error:

Failed to set property 'Windows.UI.Xaml.Controls.ContentControl.Content'

I tried to define the resource in different places (app.xaml, separate resource files, etc.), but every time I get the same error.

So, I have a few questions:

  • If possible in WinRT XAML? Am I just doing something stupid?
  • Is there any other way to arbitrarily content resources such as path data? (I had some limited success by defining a style for the Path element, adjusting the path information to the setter, but it doesn't seem to bind when going to the page. This is another problem though ...)
+6
source share
2 answers

Typically, a resource is a common "single instance", and the various XAML elements refer to one common instance. I'm not sure why the Designer suggests that this will work (except for his legacy in supporting several types of "XAML"). In the case of TextBlock , although this is slightly different, since you want the Element instance to be replicated and created several times (it may be placed in several ContentControl , for example).

WPF had a function to do this work in the special x:Shared attribute. You must set this value to false to indicate that the Resource not been split and that each resource request should return a new instance. WinRT does not have the same function.

However, you might think about supported work.

One option is to use a template instead of replacing the content directly, as you tried:

 <Page.Resources> <Style x:Name="replacement" TargetType="ContentControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ContentControl"> <TextBlock FontSize="100" Foreground="Red">Hello!</TextBlock> </ControlTemplate> </Setter.Value> </Setter> </Style> </Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <ContentControl Style="{StaticResource replacement}"></ContentControl> </Grid> 

Syntactically, this is a little longer, but functionally, it should be the same results.

Without x:Shared you are limited by the ability to bind to resources, which are internal data types, for example x:string (as shown in the example below):

 <Page.Resources> <x:String x:Key="tbResource">The Text!</x:String> </Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" > <ContentControl Content="{StaticResource tbResource}" ></ContentControl> </Grid> 
+6
source

My guess is that you are using Visual Studio 2012. There were other similar errors in 2012 that were resolved in Visual Studio 2013. Your code that works perfectly works fine in Visual Studio 2013.

enter image description here

Good luck

0
source

All Articles