views:

168

answers:

2

How do I add my property called weight to an image and use it like this:?

myImage.weight

(assuming i have already defined myImage in XAML)

here's my code:

public partial class MainWindow : Window
{
    public double Weight
    {
        get
        {
            return (double)GetValue(WeightProperty);
        }
        set
        {
            SetValue(WeightProperty, value);
        }
    }
    public static readonly DependencyProperty WeightProperty = DependencyProperty.Register("Weight", typeof(Double), typeof(Image));


    public MainWindow()
    {
        this.InitializeComponent();
                    myImage.Weight = 2;'

here the last line doesn't work because the property Weight does not attach to myImage.

This below also doesn't work in XAML:

<Image x:Name="myImage" Weight="2" />
+2  A: 

You need to create an attached property:

public static double GetWeight(DependencyObject obj)
        {
            return (double)obj.GetValue(WeightProperty);
        }

        public static void SetWeight(DependencyObject obj, double value)
        {
            obj.SetValue(WeightProperty, value);
        }

        public static readonly DependencyProperty WeightProperty =
            Dependenc**strong text**yProperty.RegisterAttached("Weight", typeof(double), typeof(MainWindow));

You can then use this in the XAML as below:

<Image x:Name="myImage" MainWindow.Weight="2" />

I would generally put the attached property on something other than MainWindow though.

You can then access the value of the property in code through:

    double weight = (double)myImage.GetValue(MainWindow.Weight);
    myImage.SetValue(MainWindow.Weight, 123.0);
Steve Greatrex
A: 

I would recommend just inheriting the Image class and adding your new dependency property.

Eg.

public class MyImage : System.Windows.Controls.Image
{
    public double Weight
    {
        get { return (double)GetValue(WeightProperty); }
        set { SetValue(WeightProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Weight.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty WeightProperty =
        DependencyProperty.Register("Weight", typeof(double), typeof(MyImage), new UIPropertyMetadata(0.0));

}

Then in your XAML code, with this local namespace:

xmlns:local="clr-namespace:[LIBRARYNAME]"

you can use:

<local:MyImage Weight="10.0"/>

It's a tad cumbersome, but I think it gives you the most control.

Alastair Pitts
I'm curious why the -1? Whoever voted it down, what was the reasoning?
Alastair Pitts
What happens when someone else on your team creates a TheirImage class with a property "AspectRatio"? What if you need to add some functionality to the Panel class, which is already subclassed by Grid, DockPanel, StackPanel etc? This is why composition is usually better than inheritance, and WPF makes composition really powerful and easy with Attached Properties
Rob Fonseca-Ensor
A fair point. Although, we haven't heard anything in the question relating to a team or anything else. If you only have 1 property to make on 1 class, then for this question the answer is correct.
Alastair Pitts
Thank u for the responses. Both points are valid, the 'inheratence' thing worked best for my situation, because making a new type of image (ImageWithWeight) makes it more obvious when using it in my application that it now has a weight property, and is different to a normal image.
dazzler77