Dear Community,
after I learnt quite a bit from my last thread, I ran into the next problem. The binding and updating inside my user control work but sadly only for a label and not for an image. I found a similar problem in a thread on the msdn (Title: user control, Image and ImageSource) but I could not adapt the solution. My guess would be that I got somehow confused by the whole bitmap/imagesource/imagestream-thing and use the wrong dependency property configuration.
To give a small overview: In my programm I bind a string property to a label in the usercontrol and a writeablebitmap to an image. In both cases I set up a dependecy property and the label changes when the string changes. The remaining problem is that my writeablebitmap does not show up in the usercontrol.
My whole problem solving goes around in circles, so I hope you can give me some fresh input.
Thank you very much in advance and best regards,
Apfelman
Below you can see the code for my usercontrol:
ViewImageData.xml
<UserControl x:Class="CollectionMVVMExample.View.ViewImageData" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:CollectionMVVMExample.View" xmlns:vm="clr-namespace:CollectionMVVMExample.ViewModel" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="520" Name="ImageDataControl"><Grid><StackPanel Margin="4"><Label x:Name="lbl_Image" FontSize="14" Content="{Binding Path=DisplayLabel, ElementName=ImageDataControl}"/><Image x:Name="img_Image" Height="424" Source="{Binding Path=BMPSource, ElementName=ImageDataControl}"/></StackPanel></Grid></UserControl>
ViewImageData.xaml.cs
using CollectionMVVMExample.ViewModel; using System; using System.Drawing; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; namespace CollectionMVVMExample.View { /// <summary> /// Interaktionslogik für ViewImageData.xaml /// </summary> public partial class ViewImageData : UserControl { #region DependencyProperty public String DisplayLabel { get { return (String)GetValue(DisplayLabelProperty); } set { SetValue(DisplayLabelProperty, value); } } // Using a DependencyProperty as the backing store for Source. This enables animation, styling, binding, etc... public static readonly DependencyProperty DisplayLabelProperty = DependencyProperty.Register("DisplayLabel", typeof(String), typeof(ViewImageData), null); public WriteableBitmap BMPSource { get { return (WriteableBitmap)GetValue(BMPSourceProperty); } set { SetValue(BMPSourceProperty, value); } } // Using a DependencyProperty as the backing store for Source. This enables animation, styling, binding, etc... public static readonly DependencyProperty BMPSourceProperty = DependencyProperty.Register("BMPSource", typeof(WriteableBitmap), typeof(ViewImageData), null); #endregion #region Constructor public ViewImageData() { InitializeComponent(); } #endregion } }
Snippet out of my MainWindow.xaml
<view:ViewImageData x:Name="uc_IR_ViewModel" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" BMPSource="{Binding IR_ViewModel.DisplayImage}" Content="{Binding IR_ViewModel.ImageLabel}" />