Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Generic.xaml vs MyCustomControl.xaml RSS

10 replies

Last post Sep 30, 2009 01:47 PM by rjacobs

(0)
  • happyfirst

    happyfirst

    Member

    2 Points

    27 Posts

    Generic.xaml vs MyCustomControl.xaml

    Aug 04, 2009 03:26 PM | LINK

    I got my custom image button control working by placing it's control template into generic.xaml. But I have a bunch of controls I'm working on and don't want them all to be in generic.xaml.  Is there a way for me to specify each custom control's template in it's own xaml file?

  • David Kelley

    David Kelley

    Member

    16 Points

    3 Posts

    Re: Generic.xaml vs MyCustomControl.xaml

    Aug 04, 2009 03:56 PM | LINK

    you have have the template in the control's xaml. 

    User control

    David J Kelley
    User eXperience Architect, Silverlight MVP
    253. 653. 9133 (c)
    www.HackingSilverlight.net | www.InteractSeattle.org

    Twit: @DavidJKelley
  • happyfirst

    happyfirst

    Member

    2 Points

    27 Posts

    Re: Generic.xaml vs MyCustomControl.xaml

    Aug 04, 2009 04:23 PM | LINK

    Thanks but what does that mean?

    This is what I have in my Generic.xaml:

    <ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"  
      xmlns:local="clr-namespace:SilverlightFramework.Controls;assembly=SilverlightFramework"    
    >
        <Style TargetType="local:ImageButton" >
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="local:ImageButton">
                        <Grid x:Name="RootElement" >
                             <Image x:Name="Image" Source="{TemplateBinding ImageSource}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Margin="1,1,1,1" HorizontalAlignment="Center" VerticalAlignment="Center" ></Image>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>

    and it works. Then I remove the above from generic.xaml and I place this into ImageButton.xaml:

    <Button x:Class="SilverlightFramework.Controls.ImageButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SilverlightFramework.Controls;assembly=SilverlightFramework"    
        >
        <Button.Template>
            <ControlTemplate TargetType="local:ImageButton">
                <Grid x:Name="RootElement" >
                    <Image Source="{TemplateBinding ImageSource}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"  ></Image>
                </Grid>
            </ControlTemplate>
        </Button.Template>       
    </Button>

    and the button stops working. It won't show the image. What am I doing wrong?

  • Kaushlendra.Singh

    Kaushlendra....

    Member

    150 Points

    33 Posts

    Re: Generic.xaml vs MyCustomControl.xaml

    Aug 05, 2009 07:05 AM | LINK

    Hey!

    you can do the same in your xamls as well by creating a UserControl.Resource just like Resources in Generic.xaml

    <UserControl.Resources>  
      <Style x:Key="ButtonStyle1" TargetType="Button">
       <Setter Property="Template">
        <Setter.Value>
         <ControlTemplate TargetType="Button">
          <Grid>
           <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="FocusStates">
             <VisualState x:Name="Focused"/>
             <VisualState x:Name="Unfocused"/>
            </VisualStateGroup>
            <VisualStateGroup x:Name="CommonStates">
             <VisualState x:Name="Normal"/>
             <VisualState x:Name="MouseOver"/>
             <VisualState x:Name="Pressed"/>
             <VisualState x:Name="Disabled"/>
            </VisualStateGroup>
           </VisualStateManager.VisualStateGroups>
           <Image Source="tennisbal.jpg"/>
           <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
          </Grid>
         </ControlTemplate>
        </Setter.Value>
       </Setter>
      </Style>  
     </UserControl.Resources>

     <Grid x:Name="LayoutRoot" Background="White">
      <Button Height="100" HorizontalAlignment="Left" Style="{StaticResource ButtonStyle1}" VerticalAlignment="Top" Width="100" Content="Button"/>
     </Grid>

    In this way you can have Resources specific to your Document.

     I hope this answers your query [:)]

    Kaushlendra Singh
    Software Engineer
    GrapeCity India

    PS. Please "Mark as Answer" if this post answered your question. :)
  • happyfirst

    happyfirst

    Member

    2 Points

    27 Posts

    Re: Generic.xaml vs MyCustomControl.xaml

    Aug 05, 2009 01:27 PM | LINK

    Thanks, but I can't get that to work either. Remeber, this is NOT a user control, it's a custom button class inheriting from button. Based on your suggestion, I tried the following in my ImageButton.xaml but it still didn't work.

    <Button x:Class="SilverlightFramework.Controls.ImageButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
        xmlns:local="clr-namespace:SilverlightFramework.Controls;assembly=SilverlightFramework"    
        >
         <Button.Resources>
            <Style TargetType="local:ImageButton" >
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="local:ImageButton">
                            <Grid x:Name="RootElement" >
                                <Border x:Name="Border" Background="Transparent" BorderBrush="Transparent" BorderThickness="1,1,1,1" HorizontalAlignment="Center" VerticalAlignment="Center" >
                                    <Image x:Name="Image" Source="{TemplateBinding ImageSource}"  Margin="1,1,1,1" HorizontalAlignment="Center" VerticalAlignment="Center" Cursor="Hand" ></Image>
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
         </Button.Resources>
    </Button>

  • Amanda Wang - MSFT

    Amanda Wang...

    All-Star

    17236 Points

    1466 Posts

    Microsoft

    Re: Generic.xaml vs MyCustomControl.xaml

    Aug 10, 2009 12:57 PM | LINK

    Hi,

    We do not think you can try to place the custome control's xaml code in the separated file, the generic.xaml is necessary.

    You have to place the custome control's UI in the generic.xaml file.

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • rmcsharry

    rmcsharry

    Member

    380 Points

    277 Posts

    Re: Generic.xaml vs MyCustomControl.xaml

    Aug 28, 2009 09:37 PM | LINK

    I found this post which works, but seems to break the ability to use Blend on SOME custom controls; can't figure out why since it is fine with others:

    http://www.codeproject.com/KB/silverlight/CustomControlTechnique.aspx

    Regards,
    Richard

    --------------------------------------
    If you can meet with triumph and disaster and treat those two imposters just the same...then you'll be a coder my son.
  • rmcsharry

    rmcsharry

    Member

    380 Points

    277 Posts

    Re: Generic.xaml vs MyCustomControl.xaml

    Aug 29, 2009 09:08 PM | LINK

    Found out you can use Merged dictionaries now in SL3 RTW, yay!

    http://silverlight.net/forums/t/123469.aspx

     

    --------------------------------------
    If you can meet with triumph and disaster and treat those two imposters just the same...then you'll be a coder my son.
  • rmcsharry

    rmcsharry

    Member

    380 Points

    277 Posts

    Re: Generic.xaml vs MyCustomControl.xaml

    Sep 08, 2009 05:14 PM | LINK

    Merged dictionaries seem to solve half the problem. Does anyone have an answer to this problem (ie. creating Custom Controls WITHOUT using generic.xaml)?

    Thanks,
    Richard

    --------------------------------------
    If you can meet with triumph and disaster and treat those two imposters just the same...then you'll be a coder my son.
  • Merlinox

    Merlinox

    Member

    6 Points

    20 Posts

    Re: Re: Generic.xaml vs MyCustomControl.xaml

    Sep 30, 2009 09:18 AM | LINK

    Is not a solution to edit complex generic.xaml in Blend?

    Merlinox (Italy)
    www.merlinox.com