PDA

Click to See Complete Forum and Search --> : Setting a property of a control in another project to a resource of the local project


Hamish
Feb 9th, 2009, 02:51 PM
I hope this is easier to explain than it is to describe, but I doubt it.
I'm still working on the Canvas version of the PictureBox_ColorMap (http://www.vbforums.com/showthread.php?t=463553&highlight=picturebox_colormap).
Here's the code for my class:


Imports System.Windows.Controls
Imports System.Windows.Media.Imaging
Imports System.Drawing
Imports System.Windows.Input

Public Class ColorMapCanvas
Inherits Canvas

Private _ColorMap As Bitmap
Private _RaiseEventOnMouseMove As Boolean = True
Private _RaiseEventOnMouseLeftButtonDown As Boolean = True

Public Event ColorMapMouseLeftButtonDown(ByVal sender As Object, ByVal ReturnedColor As Color)
Public Event ColorMapMouseMove(ByVal sender As Object, ByVal ReturnedColor As Color)

Public Property ColorMap() As Bitmap
Get
Return _ColorMap
End Get
Set(ByVal value As Bitmap)
_ColorMap = value
End Set
End Property

Public Property RaiseEventOnMouseMove() As Boolean
Get
Return _RaiseEventOnMouseMove
End Get
Set(ByVal value As Boolean)
_RaiseEventOnMouseMove = value
End Set
End Property

Public Property RaiseEventOnMouseLeftButtonDown() As Boolean
Get
Return _RaiseEventOnMouseLeftButtonDown
End Get
Set(ByVal value As Boolean)
_RaiseEventOnMouseLeftButtonDown = value
End Set
End Property

Protected Overrides Sub OnMouseLeftButtonDown(ByVal e As MouseButtonEventArgs)
Dim p As System.Windows.Point = e.GetPosition(Me)
MyBase.OnMouseLeftButtonDown(e)
If _RaiseEventOnMouseLeftButtonDown Then
If Not _ColorMap Is Nothing AndAlso p.X <= _ColorMap.Width AndAlso p.Y <= _ColorMap.Height Then
RaiseEvent ColorMapMouseLeftButtonDown(Me, _ColorMap.GetPixel(CInt(p.X), CInt(p.Y)))
Else
RaiseEvent ColorMapMouseLeftButtonDown(Me, Nothing)
End If
End If
End Sub

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
Dim p As System.Windows.Point = e.GetPosition(Me)
MyBase.OnMouseMove(e)
If _RaiseEventOnMouseMove Then
If Not _ColorMap Is Nothing AndAlso p.X <= _ColorMap.Width AndAlso p.Y <= _ColorMap.Height Then
RaiseEvent ColorMapMouseMove(Me, _ColorMap.GetPixel(CInt(p.X), CInt(p.Y)))
Else
RaiseEvent ColorMapMouseMove(Me, Nothing)
End If
End If
End Sub

End Class


Now in my XAML (in another project, I keep my custom controls together in a seperate project) I have this:

<Controls:ColorMapCanvas x:Name="MapCanvas" Width="8980" Height="5160">
<Canvas.Background>
<ImageBrush x:Name="CanvasBrush" ImageSource="/Resources/TempMainMap.png" ScrollViewer.CanContentScroll="True"/>
</Canvas.Background>
</Controls:ColorMapCanvas>


I want to add setting the ColorMap property of my ColorMapCanvas control to use another resource in the same project as the XAML. I tried using:

<Controls:ColorMapCanvas x:Name="MapCanvas" ColorMap="/Resources/TempColorMap.png" Width="8980" Height="5160">

But that gives me an error "Parameter is not valid." on the ColorMap property.
I can think of 2 reasons this may happen, but can't think of a solution for either.


The "/Resources/TempColorMap.png" isn't a Bitmap, but a BitmapSource.
The ColorMapCanvas control can't access the image to be used because it's in another project.


So, can anyone tell me which of these (or something I haven't thought of) it is, and what to do about it?

Hamish
Feb 10th, 2009, 05:17 AM
Update:

I solved the issue where I couldn't assign a value to the ColorMap property from the XAML by adding this line to the class definition:

Public Shared ReadOnly ColorMapProperty As Windows.DependencyProperty = Windows.DependencyProperty.Register("ColorMap", GetType(Bitmap), _
GetType(ColorMapCanvas), New Windows.PropertyMetadata(Nothing))
I still don't get this DependencyProperty thing, but that's something I can remedy myself. :)

Next problem is that at run-time I get an XamlParseException:
"Cannot convert string '/Resources/TempColorMap.png' in attribute 'ColorMap' to object of type 'System.Drawing.Bitmap'. ImageConverter cannot convert from Syste.String."

So it's still either or both of the problems I suggested in the previous post. Any help would be appreciated!

vbNeo
Feb 10th, 2009, 05:56 AM
"Cannot convert string '/Resources/TempColorMap.png' in attribute 'ColorMap' to object of type 'System.Drawing.Bitmap'. ImageConverter cannot convert from Syste.String."


That's because you're passing a String to your property and not a Bitmap. You should use a value converter to convert the string to a bitmap; see this reference:

http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

If you need help on using it I can give you a small example.

Hamish
Feb 11th, 2009, 03:39 AM
Oh dear.... another C# example. :)
I'll see if I get it!

Hamish
Feb 11th, 2009, 03:53 AM
Hmmm, I think I understand what's going on. But I don't want to use data binding, I just want to set the value of the property once and for all.
Any suggestions for that?

vbNeo
Feb 11th, 2009, 04:12 AM
Hmmm, I think I understand what's going on. But I don't want to use data binding, I just want to set the value of the property once and for all.
Any suggestions for that?

Instead of having the type of your property as a Bitmap let it be an Object, and just cast it back and forth when you need it.

That'll ensure it won't raise an exception when xaml throws a String at it.

Hamish
Feb 17th, 2009, 09:17 AM
I had other things on my mind for a while, but I'm now trying to get this working again. I tried defining my property as an Object, but can't get it to create a Bitmap from the String in the ColorMapCanvas class. Can you give an example?

In the meantime I tried to add a resource to my window like this:


<Window x:Class="VUI_Main" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Drawing="clr-namespace:System.Drawing;assembly=System.Drawing>
<Window.Resources>
<Drawing:Bitmap x:Key="TempColorMap" />
</Window.Resources>


But I can't set a property specifying the path for the Bitmap... :mad:
I could of course just set the ColorMap property from the VUI_Main.Loaded event like this:


If MapCanvas.ColorMap Is Nothing Then MapCanvas.ColorMap = My.Resources.TempColorMap


This works, but I'd prefer to do it all in the XAML.