Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Custom control doesn't take data from generic.xaml RSS

4 replies

Last post Jun 29, 2009 09:50 AM by bgnSL

(0)
  • bgnSL

    bgnSL

    Member

    17 Points

    28 Posts

    Custom control doesn't take data from generic.xaml

    Jun 26, 2009 03:54 PM | LINK

    I'm following Jeff Prosise's MSDN tutorial (http://msdn.microsoft.com/en-us/magazine/cc721611.aspx) on creating custom controls. But when I create generic.xaml and bind it to my custom buttom, IE still shows an empty button.

     This is mybutton class

    public class MyButton : ContentControl
    {
    public MyButton()
    {
    this.DefaultStyleKey = typeof (MyButton);
    }
    }
    This is generic.xaml 
    <ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MyButton="clr-namespace:SilverlightApplication2;assembly=SilverlightApplication2">
    <Style
    TargetType="MyButton:MyButton">
    <Setter
    Property="Width"
    Value="200" />
    <Setter
    Property="Height"
    Value="100" />
    <Setter
    Property="Background"
    Value="Lavender" />
    <Setter
    Property="Template">
    <Setter.Value>
    <ControlTemplate
    TargetType="MyButton:MyButton">
    <Grid
    x:Name="RootElement">
    <Rectangle
    x:Name="BodyElement"
    Width="{TemplateBinding Width}"
    Height="{TemplateBinding Height}"
    Fill="{TemplateBinding Background}"
    Stroke="Purple"
    RadiusX="16"
    RadiusY="16" />
    <TextBlock
    Text="Click Me"
    HorizontalAlignment="Center"
    VerticalAlignment="Center" />
    </Grid>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>
    </ResourceDictionary>
    And this is page.xaml 
    <UserControl x:Class="SilverlightApplication2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MyButton="clr-namespace:SilverlightApplication2;assembly=SilverlightApplication2"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
    <MyButton:MyButton Width="255" />
    </Grid>
    </UserControl>
    Also to say that Blend sees generic.xaml properly because I can edit my button.  
    It's all so simple but the style from generic.xaml does not get to the custom button. 
    Any ideas?

    generic.xaml custom control

    ---
    Click 'Mark as Answer' if my reply helped you!
    Thanks
  • rshelby

    rshelby

    Member

    305 Points

    75 Posts

    Re: Custom control doesn't take data from generic.xaml

    Jun 27, 2009 02:46 AM | LINK

    Try adding a key name to your style.

     For example:

    <Style x:Key="myButtonStyle" TargetType="MyButton:MyButton">

    Then reference the name from your button control like so:

    <MyButton:MyButton Style="{StaticResource myButtonStyle}" />

    Hope this helps.

    rshelby

    Ryan Shelby
  • SharpGIS

    SharpGIS

    Contributor

    4835 Points

    832 Posts

    Re: Custom control doesn't take data from generic.xaml

    Jun 27, 2009 04:50 AM | LINK

     rshelby: The approach you describe is for defining custom styles in resources. But the above is for defining the default template on custom controls, and you dont define key names in generic.xaml like this.

     

    I wonder if the prefix MyButton is confusing Silverlight, since this is the same name as the class. Try changing it to something else (I usually use local: for the assembly namespace).

    --
    /Morten | Silverlight MVP | blog - twitter
    Please click on "Mark as Answer" if this answered your question.
  • Krasshirsch

    Krasshirsch

    Participant

    1152 Points

    361 Posts

    Re: Custom control doesn't take data from generic.xaml

    Jun 27, 2009 01:50 PM | LINK

    The Generic.xaml file needs to be placed inside the folder Themes, since SL 2 RTW.

    A Bro must always post bail for another Bro, unless it's out of state or, like, crazy expensive.

    Crazy Expensive Bail > (Years You've been Bros) * $100

    Alexander Wieser
    Germany
  • bgnSL

    bgnSL

    Member

    17 Points

    28 Posts

    Re: Re: Custom control doesn't take data from generic.xaml

    Jun 29, 2009 09:50 AM | LINK

    When I moved it to a folder Themes, the miracle happened :)

    Thanks.

    ---
    Click 'Mark as Answer' if my reply helped you!
    Thanks