locked
Setting Image control's Width and Height properties RRS feed

  • Question

  • Hi All,

    I have a custom control that extends from System.Windows.Controls.Image control. I need to set a default size of 50, 50 for that control (whenever a user drags and drops that custom control from the visual studio's toobox). I tried various options like:

    <Style x:Key="{x:Type local:MyImage}" TargetType="{x:Type local:MyImage}">
        <Setter Property="Width" Value
    ="100" />
        <Setter Property="Height" Value
    ="100" />
        <Setter Property="Stretch" Value
    ="None" />
    </Style>

    or in the code:

    Width = 50;
    Height = 50;
    Stretch = System.Windows.Media.Stretch.None;

    However both these options are not working! Whenever I drop my custom image to the window inside visual studio from its toolbox, it results in the default width and height of 150 and 200.

    I am not able to set my custom width and height. Also I was wondering that from where does the image control pickup these default values. I mean where are these defined. In a resource file or somewhere in the code?

    GS

    Wednesday, October 8, 2008 6:17 PM

Answers

  • Hi GS -

    You may need to use the DependencyProperty.OverrideMetadata approach to set the default values for these properties. 

    Here is a link to another thread that discusses how to set defaults for Width and Height, and it demonstrates how to add an owner to the DependencyProperty (your custom control) and set defaults for your custom control that are different from the defaults of the base class: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c4d2428c-a5bd-4688-af13-ee87bf804fc3/.

    Hope this helps,
    Brian
    Brian Schwalm • www.anythinksolutions.com
    • Marked as answer by Marco Zhou Thursday, October 16, 2008 10:17 AM
    Wednesday, October 8, 2008 7:17 PM

All replies

  • Hi GS -

    You may need to use the DependencyProperty.OverrideMetadata approach to set the default values for these properties. 

    Here is a link to another thread that discusses how to set defaults for Width and Height, and it demonstrates how to add an owner to the DependencyProperty (your custom control) and set defaults for your custom control that are different from the defaults of the base class: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c4d2428c-a5bd-4688-af13-ee87bf804fc3/.

    Hope this helps,
    Brian
    Brian Schwalm • www.anythinksolutions.com
    • Marked as answer by Marco Zhou Thursday, October 16, 2008 10:17 AM
    Wednesday, October 8, 2008 7:17 PM
  • Hi Brian,

    Thanks for your reply. But the solution link you provided is not working in my case. As WPF Image class extends directly from FrameworkElement and not the control class so  Iam not able to use that approach. Any code solution will be greatley appreciated.

    Thanks,
    GS
    Thursday, October 9, 2008 5:06 AM
  • -> However both these options are not working!

    Could you please provide a small, complete and ready-to-run example to demonstrate how explicitly setting Width and Height properties doesn't work for you?

     

    Thanks


    Monday, October 13, 2008 7:34 AM