Reputation: 188
I can't figure out how to bind my image:
<Image Source="{ Binding Path=ViewModel.MainViewModel.ProcessedImage }" Name="image1"/>
to DependencyProperty ProcessedImage, which is child element of my custom class derived from DependencyObject:
class ViewModel : DependencyObject
{
public static ViewModel MainViewModel { get; set; }
[...]
public BitmapImage ProcessedImage
{
get { return (BitmapImage)this.GetValue(ProcessedImageProperty); }
set { this.SetValue(ProcessedImageProperty, value); }
}
public static readonly DependencyProperty ProcessedImageProperty = DependencyProperty.Register(
"ProcessedImage", typeof(BitmapImage), typeof(ViewModel), new PropertyMetadata());
}
I hope You can help me out with this. I've tried different approaches but nothing seems to work.
Upvotes: 0
Views: 287
Reputation: 48
How are you setting the data context? I copied your code and added another property - ProcessedImageName with a default value of "Hello World"
public static readonly DependencyProperty ProcessedImageNameProperty = DependencyProperty.Register(
"ProcessedImageName", typeof(string), typeof(ViewModel), new PropertyMetadata("Hello World"));
public string ProcessedImageName {
get { return (string)this.GetValue(ProcessedImageNameProperty); }
set { this.SetValue(ProcessedImageNameProperty, value); }}
The I set the data context as follows:
public MainWindow()
{
InitializeComponent();
ViewModel.MainViewModel = new ViewModel();
DataContext = ViewModel.MainViewModel;
}
The I set the binding path as:
<TextBlock Text="{Binding Path=ProcessedImageName }"/>
Personally, I wouldn't continue with the static MainViewModel property and would instead just new up a ViewModel instance as so
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
The path to a property is relative to the data context so if a property is Class.PropertyName and the data context is Class then the binding path is just PropertyName.
Upvotes: 1