locked
So weird: why I can not use StaticResource of type DependencyObject as the content of ContentControl? RRS feed

  • Question

  • I've created a StaticResource of some type which is inherited from DependencyObject, and then set the Content of a Button to it. Here is what I do. It can be complied, but will throw a runtime error.

    [MainPage.xaml]

    <Page
        x:Class="Test.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Test">
        <Page.Resources>
            <local:MyClass x:Key="obj" />
            <local:AnotherClass x:Key="another" />
        </Page.Resources>
    
        <Grid  Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <!--this line works fine!-->
            <Button Content="{StaticResource obj}"/>
            
            <!--this line will throw runtime error!-->
            <Button Content="{StaticResource another}" />
        </Grid>
    </Page>

    [MainPage.xaml.cs]

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    
    namespace Test
    {
        public class AnotherClass : DependencyObject
        {
            //just an ampty class
        }
    
        public class MyClass
        {
            //just an ampty class
        }
    
    
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
            }
        }
    }

    Before I post this thread, I have found this link http://social.msdn.microsoft.com/Forums/windowsapps/en-US/9076af75-07e2-40b4-8890-6ea546d1e9de/cant-use-staticresource-as-content-of-contentcontrol?forum=silverlightbugs

    In that thread, the second reply said:

    "Content cannot be a resource. It should be either a control or a text."

    This obviously can not be true, since from the above code we can see that it is fine to set the Content of a button to a StaticResource of type other than DependencyObject or Its descendants.

    So I really want a clear explanation of what can be set to the content of ContentControl and why? I can't find any information about this on the MSDN online document do. Any idea would be appreciated!


    Monday, November 18, 2013 11:58 AM

All replies

  • Yes, I see that it compiles but does not run.  My question for you is:  what are you trying to accomplish with this code?  If you start giving it any values, does it work? 

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, November 19, 2013 3:00 PM
    Moderator
  • Hi, Matt, thanks for the quick reply!

    Unfortunately, I have to pass a dependency object to the Content property of a ContentControl. Below is all the source code. I want to create three StackPanel in a ListViewItem to display the information of the behind object. To keep code clean, I want these StackPanels use the same DataTemplate, so I put them in ContentControl's ContentTemplate.

    The following code would throw a runtime error. I can't figure out what is wrong. Thanks for your time!

    [MainPage.xaml]

    <Page x:Class="App1.MainPage"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="using:App1">
        <Page.Resources>
            <local:MyList x:Key="list" />
            
            <local:MyValueConverter x:Key="MyValueConverter" />
            
            <DataTemplate x:Key="MyCellTemplate">
                <StackPanel>
                    <TextBlock Text="{Binding Id}" />
                    <TextBlock Text="{Binding SomeObject.Name}" />
                    <TextBlock Text="{Binding Converter={StaticResource MyValueConverter}}" />
                </StackPanel>
            </DataTemplate>
    
            <DataTemplate x:Key="MyItemTemplate">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100*" />
                        <ColumnDefinition Width="100*" />
                        <ColumnDefinition Width="100*" />
                    </Grid.ColumnDefinitions>
                    <Grid.Resources>
                        <local:MyWrapper x:Key="MyWrapper_0"
                                         Id="0"
                                         SomeObject="{Binding}" />
                        <local:MyWrapper x:Key="MyWrapper_1"
                                         Id="1"
                                         SomeObject="{Binding}" />
                        <local:MyWrapper x:Key="MyWrapper_2"
                                         Id="2"
                                         SomeObject="{Binding}" />
                    </Grid.Resources>
    
                    <ContentControl Content="{StaticResource MyWrapper_0}"
                                    ContentTemplate="{StaticResource MyCellTemplate}" />
                    <ContentControl Content="{StaticResource MyWrapper_1}"
                                    ContentTemplate="{StaticResource MyCellTemplate}" />
                    <ContentControl Content="{StaticResource MyWrapper_2}"
                                    ContentTemplate="{StaticResource MyCellTemplate}" />
                </Grid>
            </DataTemplate>
        </Page.Resources>
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <ListView ItemsSource="{StaticResource list}"
                      ItemTemplate="{StaticResource MyItemTemplate}" />
    
        </Grid>
    </Page>
    

    [MainPage.xaml.cs]

    using System;
    using System.Collections.ObjectModel;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Data;
    
    
    namespace App1
    {
        public sealed class SomeClass
        {
            public string Name { get; set; }
            public string[] Info { get; set; }
    
            public SomeClass()
            {
                Info = new string[3] { "AAA", "BBB", "CCC" };
            }
        }
    
        public sealed class MyList : ObservableCollection<SomeClass>
        {
            public MyList()
            {
                this.Add(new SomeClass());
                this.Add(new SomeClass());
                this.Add(new SomeClass());
                this.Add(new SomeClass());
                this.Add(new SomeClass());
            }
        }
    
    
    
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
        }
    
        public sealed class MyWrapper : DependencyObject
        {
            public int Id { get; set; }
    
            public SomeClass SomeObject
            {
                get { return (SomeClass)GetValue(SomeObjectProperty); }
                set { SetValue(SomeObjectProperty, value); }
            }
            public static readonly DependencyProperty SomeObjectProperty =
                DependencyProperty.Register("SomeObject", typeof(SomeClass), typeof(MyWrapper), new PropertyMetadata(null));
        }
    
        public sealed class MyValueConverter : IValueConverter
        {
    
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                var wrapper = (MyWrapper)value;
                return wrapper.SomeObject.Info[wrapper.Id];
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }
    
    
    }
    


    Wednesday, November 20, 2013 8:23 AM