Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I defined a ControlTemplate within the ResourceDictionary, the ControlTemplate like that

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ControlTemplate x:Key="CTGelButton" TargetType="{x:Type Button}">
        <Grid Width="100" Height="100">
            <Ellipse Stroke="#FF000000" Fill="#FF1C46E7" x:Name="OuterEllipse">
                <Ellipse.BitmapEffect>
                    <BevelBitmapEffect BevelWidth="8" EdgeProfile="BulgedUp" Smoothness="0.745"/>
                </Ellipse.BitmapEffect>
            </Ellipse>
            <Ellipse Margin="8,8,8,8" x:Name="InnerEllipse">
                <Ellipse.Fill>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" SpreadMethod="Pad">
                        <GradientStop Color="#FF1C46E7" Offset="0"/>
                        <GradientStop Color="#FFFFFFFF" Offset="1"/>
                    </LinearGradientBrush>
                </Ellipse.Fill>
            </Ellipse>
            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}"/>
        </Grid>
    </ControlTemplate>



</ResourceDictionary>


and I want tp apply this ControlTemplate to a button, within the window.xaml code like that

Window x:Class="ControlTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

    </Window.Resources>
    
    <Grid>
        <StackPanel >

            <Button Content="Hello" Width="50" Height="50" Template="{DynamicResource CTGelButton}" Click="Button_Click" />

        </StackPanel>
    </Grid>
</Window>


but window.xaml shows warning saying "CTGelButton can not be resolved", when I run it there is no button appear.
how can I fix that? how can I use ControlTemplate as DynamicResource?
did I miss something here?
could anyone help me please?
Posted
Updated 15-Mar-12 23:32pm
v2

You need to include the original Resource Dictionary as a resource in your Window.XAML file
HTML
<Window x:Class="ControlTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="yourResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    etc


or you could include it in your App.XAML file if you want it available to all XAML files in your project.
 
Share this answer
 
Hi,

I had the same problem as you and this is the solution it worked for me:

1.- Right click on the project name and select "Unload Project".
2.- Right click on the project name and select "Edit <<Project name>>"
3.- Search the resource file name, let say MyResource. You will find something like:

XML
<Content Include="Skins\MyResource.xaml">
  <SubType>Designer</SubType>
  <Generator>MSBuild:Compile</Generator>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>



4.- Replace it with:

XML
<Content Include="Skins\MyResource.xaml">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</Content>


5.- Close the project file.
6.- Right click on the project name and select "Reload project".
7.- Cleck the problem is gone and Visual Studio can load resources at Design time.

I did not check if the solution comes for the change in the order of the XML tags or it comes due to the double "Generator" and "SubType" tags. I had no time to find it out.

I hope this may help some one.

Benjamin
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900