none
problem with DataTemplate building a PropertyGrid 'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.'

    Question

  • Hallo everybody,

    I build a propertygrid, i did this in a usercontrol class library. Everything works ok until i try to add a ColorDataTemplate. I use the colorpicker from the extended wpf toolkit. However just when i add the ColorDataTemplate the program wont run and i getthe following error: 

    'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '62' and line position '10'.

    Strange enough i have i this same code working in a project. The only difference in there is that, its not a class library but just code into the project.

    Here is the code, i can also upload the whole project if neede:

    <usercontrol x:class="GOOO.PropertyGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:exttoolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended" xmlns:l="clr-namespace:GOOO" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <usercontrol.resources>
    
            
            <datatemplate x:key="DefaultDataTemplate">
                <grid height="Auto" margin="4">
                    <grid.columndefinitions>
                        <columndefinition minwidth="120" sharedsizegroup="Key" width="Auto">
                        <columndefinition width="*">
                    </columndefinition></columndefinition></grid.columndefinitions>
                    <textblock fontweight="Normal" horizontalalignment="Right" text="{Binding Name}"></textblock>
                    <textbox grid.column="1" margin="8,0" text="{Binding Value }"></textbox>
                </grid>
            </datatemplate>
    
            
            <datatemplate x:key="BooleanDataTemplate">
                <grid height="Auto" margin="4">
                    <grid.columndefinitions>
                        <columndefinition minwidth="120" sharedsizegroup="Key" width="Auto">
                        <columndefinition width="*">
                    </columndefinition></columndefinition></grid.columndefinitions>
                    <textblock fontweight="Normal" horizontalalignment="Right" text="{Binding Name}"></textblock>
                    <checkbox grid.column="1" ischecked="{Binding Value}" margin="8,0"></checkbox>
                </grid>
            </datatemplate>
    
            
            <datatemplate x:key="EnumDataTemplate">
                <grid height="Auto" margin="4">
                    <grid.columndefinitions>
                        <columndefinition minwidth="120" sharedsizegroup="Key" width="Auto">
                        <columndefinition width="*">
                    </columndefinition></columndefinition></grid.columndefinitions>
                    <textblock fontweight="Normal" horizontalalignment="Right" text="{Binding Name}"></textblock>
                    <combobox grid.column="1" horizontalalignment="Stretch" itemssource="{Binding PropertyType, 
                              Converter={l:EnumToListConverter}}" margin="8,0" selectedvalue="{Binding Value}"></combobox>
                </grid>
            </datatemplate>
    
            
            <datatemplate x:key="ColorDataTemplate">
                <grid height="Auto" margin="4">
                    <grid.columndefinitions>
                        <columndefinition minwidth="120" sharedsizegroup="Key" width="Auto">
                        <columndefinition width="*">
                    </columndefinition></columndefinition></grid.columndefinitions>
                    <textblock fontweight="Normal" text="{Binding Name}"></textblock>
                    <exttoolkit:colorpicker grid.column="1" selectedcolor="{Binding Value, Converter={l:ColorToBrushConverter}}"></exttoolkit:colorpicker>
                </grid>
            </datatemplate>
    
            
            <l:propertydatatemplateselector booleandatatemplate="{StaticResource BooleanDataTemplate}" colordatatemplate="{StaticResource ColorDataTemplate}" defaultndatatemplate="{StaticResource DefaultDataTemplate}" enumdatatemplate="{StaticResource EnumDataTemplate}" x:key="templateSelector"></l:propertydatatemplateselector>
    
        </usercontrol.resources>
    
        <grid>
            <grid.rowdefinitions>
                <rowdefinition height="15"></rowdefinition>
                <rowdefinition height="*"></rowdefinition>
                <rowdefinition height="75"></rowdefinition>
            </grid.rowdefinitions>
    
            <textblock datacontext="{Binding ElementName=myListBox}" grid.row="0" horizontalalignment="Stretch" text="{Binding Name}" verticalalignment="Top"></textblock>
    
            <listbox borderbrush="Transparent" grid.issharedsizescope="True" grid.row="1" horizontalcontentalignment="Stretch" itemssource="{Binding}" itemtemplateselector="{StaticResource templateSelector}" name="myListBox" verticalcontentalignment="Stretch">
                <listbox.groupstyle>
                    <groupstyle>
                        <groupstyle.containerstyle>
                            <style targettype="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander>
                                                <Expander.Header>
                                                    <StackPanel Orientation="Horizontal" >
                                                        <TextBlock Text="{Binding Path=Name}" ></TextBlock>
                                                    </StackPanel>
                                                </Expander.Header>
                                                <ItemsPresenter ></ItemsPresenter>
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </style>
                        </groupstyle.containerstyle>
                    </groupstyle>
                </listbox.groupstyle>
            </listbox>
    
            <textblock datacontext="{Binding ElementName=myListBox, Path=SelectedItem}" grid.row="2" horizontalalignment="Stretch" name="TextBlockDescription" text="{Binding Description}" verticalalignment="Top"></textblock>
    
        </grid>
    
    </usercontrol>

    And the template selector and color converter:

    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Media;
    
    namespace GOOO
    {
        public class PropertyDataTemplateSelector : DataTemplateSelector
        {
            public DataTemplate DefaultnDataTemplate { get; set; }
            public DataTemplate BooleanDataTemplate { get; set; }
            public DataTemplate EnumDataTemplate { get; set; }
            public DataTemplate ColorDataTemplate { get; set; }
    
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                // DependencyPropertyInfo dpi = item as DependencyPropertyInfo;
                DependencyPropertyInfo dpi = item as DependencyPropertyInfo;
    
                if (dpi.PropertyType == typeof(bool))
                {
                    return BooleanDataTemplate;
                }
                if (dpi.PropertyType.IsEnum)
                {
                    return EnumDataTemplate;
                }
                if (dpi.PropertyType == typeof(Brush))
                {
                    return ColorDataTemplate;
                }
    
                return DefaultnDataTemplate;
            }
        }
    }
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Data;
    using System.Globalization;
    using System.Windows.Markup;
    using System.Windows.Media;
    
    namespace GOOO
    {
        public class ColorToBrushConverter : MarkupExtension, IValueConverter
        {
    
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                Type type = value as Type;
                if (type != null && type == typeof(Color))
                {
                   return new SolidColorBrush((Color) value); 
                }
    
                return null;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                BrushConverter conv = new BrushConverter();
                Brush brush = conv.ConvertFromString(value.ToString()) as Brush;
                return brush;
            }
    
    
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                return this;
            }
        }
    }


    Jc


    • Edited by bochelie Sunday, February 19, 2012 3:17 PM
    Sunday, February 19, 2012 3:14 PM

Answers

  • It seems like there is no answer. I started building my own colorpicker and wont use that one any more. So i will close this thread.

    Jc

    • Marked as answer by bochelie Friday, February 24, 2012 9:56 AM
    Friday, February 24, 2012 9:56 AM

All replies

  • Hi,

    The reason its throwing an error is because the datatemplates have big syntax issues.

    1) There is no uppercase e.g. "textbox" should be "TextBox". All grids, columdefinitions, controls etc have this issue.

    2) Each datatemplate defines a grid and columndefinition. In each datatemplate there is a column within a column  

    <grid.columndefinitions> <columndefinition minwidth="120" sharedsizegroup="Key" width="Auto"> <columndefinition width="*"></columndefinition>

    <

    You need to take out this column.

    I fixed the color data template to give you something to start with - 

    <DataTemplate x:Key="ColorDataTemplate">
                <Grid Height="Auto" Margin="4">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="120" SharedSizeGroup="Key" Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="*">
                        </ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock FontWeight="Normal" Text="{Binding Name}"></TextBlock>
                    <exttoolkit:ColorPicker Grid.Column="1" SelectedColor="{Binding Value, Converter={l:ColorToBrushConverter}}"></exttoolkit:colorpicker>
                </Grid>
            </DataTemplate>

    Hopefully these 2 suggestions will resolve your issue and you can start using the datatemplates in your application. 

    Sunday, February 19, 2012 7:29 PM
  • Hiya

    As you can see above, the msdn forum editor has stripped all your xaml formatting, so trying to recreate the issue from your code snippets is not possible.

    If you posted a complete example, I may have time tonight or tomorrow. 

    However, my first piece of advice, considering the error message, would be to start removing StaticResource references from your xaml, one by one, till you find which is throwing the error. It doesn't look like you need to, but changing to DynamicReference may also help? Or does moving it to Application.Resources help?

    Regards,
    Pete


    #PEJL

    Sunday, February 19, 2012 7:49 PM
    Moderator
  • Okay, 

    I didnt take the xaml editor removing formatting into account.

    then my only suggestion is still the columns within columns. That is definetly going to cause an error.

    you still need to declare them outside each other.

    Sunday, February 19, 2012 7:53 PM
  • Hi,

    The reason its throwing an error is because the datatemplates have big syntax issues.

    1) There is no uppercase e.g. "textbox" should be "TextBox". All grids, columdefinitions, controls etc have this issue.

    2) Each datatemplate defines a grid and columndefinition. In each datatemplate there is a column within a column  

     <grid.columndefinitions>
                        <columndefinition minwidth="120" sharedsizegroup="Key" width="Auto">
                        <columndefinition width="*"></columndefinition><</columndefinition></grid.columndefinitions>

    You need to take out this column.

    I fixed the color data template to give you something to start with - 

    <datatemplate x:Key="ColorDataTemplate">
                <grid Height="Auto" Margin="4">
                    <grid.columndefinitions>
                        <columndefinition MinWidth="120" SharedSizeGroup="Key" Width="Auto"></columndefinition>
                        <columndefinition Width="*">
                        </columndefinition>
                    </grid.columndefinitions>
                    <textblock FontWeight="Normal" Text="{Binding Name}"></textblock>
                    <exttoolkit:colorpicker Grid.Column="1" SelectedColor="{Binding Value, Converter={l:ColorToBrushConverter}}"></exttoolkit:colorpicker>
                </grid>
            </datatemplate>

    Hopefully these 2 suggestions will resolve your issue and you can start using the datatemplates in your application. 

    I don't think this is his actual code, it's caused by the MSDN editor. Both the lower casing and the tag re-arranging.
    It's happened to me many times, especially if I try to edit a post :/
    I believe there's an active thread about it in the forum feedback section.

    Regards,
    Pete


    #PEJL

    Sunday, February 19, 2012 7:54 PM
    Moderator
  • Yes, its the MSDN editor thats doing crazy.

    I placed the project on my skyDrive. I know there is something wrong with the colorPicker im using. But if i use it in my project it works in the same code without problems.

    https://skydrive.live.com/redir.aspx?cid=da6abe46d5bce7be&resid=DA6ABE46D5BCE7BE!227&parid=DA6ABE46D5BCE7BE!225

    By the way. I like the propertyGrid of the Extended WPF ToolBox. Im using there colorpicker. But what about that licence. Do i realy need to place the license in my project as wel? I dont like that ideaa.


    Jc

    Sunday, February 19, 2012 9:44 PM
  • Hi bochelie,

    I try to download your sapmle code from skydrive, but failed, please make sure the sample is exist at the right place.

    please let me know the result of your trouble shooting at your convenience.

    Thank you.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, February 21, 2012 8:41 AM
    Moderator
  • It seems like there is no answer. I started building my own colorpicker and wont use that one any more. So i will close this thread.

    Jc

    • Marked as answer by bochelie Friday, February 24, 2012 9:56 AM
    Friday, February 24, 2012 9:56 AM
  • Hi bochelie,

    Sorry for that I have no time to test it these days.

    If you still want to resolved this issue, you can reopen this thread again and post the test sample here and inform me.

    Sorry for my late reply.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 27, 2012 4:31 AM
    Moderator