locked
Style for UserControl RRS feed

  • Question

  • Hello,

    I define a Usercontrol then define a Style

        <Style x:Key="key" TargetType="{x:Type controls:MyStyle}">
            <Setter Property="Caption" Value="More"/>            
        </Style>

    However, it keeps say that MyStyle is not found (I already defined the controls namespace)

    Do you have some suggestions about this?

    Thank you

    Ball

    Thursday, June 7, 2012 4:54 PM

Answers

  • @Ball - is this a user control or a custom control?  If a custom control (i.e, Templated Control item template) the TargetType would just be the type..don't add x:Type there.  If a user control, you can't style those.

    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)

    Thursday, June 7, 2012 6:56 PM

All replies

  • @Ball - is this a user control or a custom control?  If a custom control (i.e, Templated Control item template) the TargetType would just be the type..don't add x:Type there.  If a user control, you can't style those.

    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)

    Thursday, June 7, 2012 6:56 PM
  • @Tim: It's a user control. 
    My problem is that my control is used at many place with the same caption. I would like to style with so that I don't repeat the string caption.

    In this case,  I guess I can do it by binding the caption to a constant in Application resources.

    Thank you for your information.

    Ball

    Friday, June 8, 2012 7:21 AM
  • @Ball - you don't need to be able to use styles in that case.  In your UserControl, add a DependencyProperty (there is a code snippet 'propdp') for your Caption property.  Then when your project uses your UserControl:

    <my:FooUserControl Caption="Hello world" />

    Then in your logic of your user control you'd have to know how to set it wherever you need.

    Hope this helps!


    Tim Heuer | Program Manager, XAML | http://timheuer.com/blog | @timheuer

    (if my post has answered your question, please consider using the 'mark as answer' feature in the forums to help others)

    Friday, June 8, 2012 2:39 PM