Can someone help me with my problem:
I want to pass a value to a class that contains a User control using Dependency Property.
I have created a program that Draws Pan And Zoom in an image.
My Pan and Zoom are based from Pan & Zoom Image
During the almost three years in the Office. I created 4 program similar to this one.
So i want it to be reusable instead of copying the code again.
I created a c# class library that contains the Pan and zoom
The class name for the pan and zoom is ZoomBorder
and i added a dependency property which is used to check if Pan or Drawing is going to be executed
public bool IsDrawing
{
get { return (bool)GetValue(IsDrawingProperty); }
set { SetValue(IsDrawingProperty, value); }
}
// Using a DependencyProperty as the backing store for IsDrawing. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsDrawingProperty =
DependencyProperty.Register("IsDrawing", typeof(bool), typeof(ZoomBorder),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null));
Then in that Class library i added Wpf User control and named it Viewer
And i also added Image control in the said user control
Here is the xaml code inside the class library
DataContext="{Binding RelativeSource={RelativeSource Self}}"
<e:ZoomBorder x:Name="viewer" IsDrawing="{Binding IsDrawing}" >
<Grid>
<Image Source="{Binding ImageSource}" />
</Grid>
</e:ZoomBorder>
And below is the code behind of Viewer
public partial class Viewer : UserControl
{
public Viewer()
{
InitializeComponent();
}
/// <summary>
/// This is for setting the image in the Viewer
/// </summary>
public string ImageSource
{
get { return (string)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
// Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(string), typeof(Viewer), new PropertyMetadata(null));
public bool IsDrawing
{
get { return (bool)GetValue(IsDrawingProperty); }
set { SetValue(IsDrawingProperty, value); }
}
// Using a DependencyProperty as the backing store for IsDrawing. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsDrawingProperty =
DependencyProperty.Register("IsDrawing", typeof(bool), typeof(Viewer),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null));
}
And here is how i implemented the class. But It doesn't show the Image
<DrawPanZoomResizeMove:Viewer IsDrawing="{Binding IsDrawing}" ImageSource="{Binding ImagePath}" />
But when i do this it's working
<DrawPanZoomResizeMove:Viewer IsDrawing="{Binding IsDrawing}" ImageSource="Image.jpg" />
The image is shown.
And in order to check if the Binding works. I added an Image control (Not in the Library, but in the main project). Then load the Image from the binding. And it worked.
This is the error message when i use a binding
System.Windows.Data Error: 40 : BindingExpression path error: 'ImagePath' property not found on 'object' ''Viewer' (Name='')'. BindingExpression:Path=ImagePath; DataItem='Viewer' (Name=''); target element is 'Viewer' (Name=''); target property is 'ImageSource' (type 'String')
Can someone point the error in my codes. I've been trying to solve this for hours now. But i can't still solve the problem
Update:
I changed the binding into
<DrawPanZoomResizeMove:Viewer IsDrawing="{Binding IsDrawing}" ImageSource="{Binding ImageSource}" />
But the problem is still there. The image doesn't load when i use Binding
Update:
I added this in the MainWindow.xaml
<Window.Resources>
<vm:ViewModel x:Key="vm"/>
</Window.Resources>
Then i changed the code into
<DrawPanZoomResizeMove:Viewer IsDrawing="{Binding IsDrawing}" ImageSource="{Binding ImageSource, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Source={StaticResource vm}}" />
The Image loaded on design mode.
But when i run the program. The error become blank
and give me error
System.Windows.Data Error: 40 : BindingExpression path error: 'ImageSource' property not found on 'object' ''ViewModel' (HashCode=26148945)'. BindingExpression:Path=ImageSource; DataItem='ViewModel' (HashCode=26148945); target element is 'Viewer' (Name=''); target property is 'ImageSource' (type 'String')
If i understand correctly the error. The Image source is not found in the viewmodel. And which is right because the ViewModel
in my CLASS library
doesn't have a ImageSource property Because the Dependency Property is in Viewer.xaml. So what is the right way to solve this?