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" />
I would recommend just inheriting the Image class and adding your new dependency property.
Eg.
Then in your XAML code, with this local namespace:
you can use:
It’s a tad cumbersome, but I think it gives you the most control.