Results 1 to 7 of 7

Thread: Setting a property of a control in another project to a resource of the local project

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Setting a property of a control in another project to a resource of the local project

    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.
    Here's the code for my class:

    Code:
    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:
    Code:
    <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:

    Code:
    <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.

    1. The "/Resources/TempColorMap.png" isn't a Bitmap, but a BitmapSource.
    2. 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?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Re: Setting a property of a control in another project to a resource of the local project

    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:

    Code:
    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!

  3. #3
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Re: Setting a property of a control in another project to a resource of the local pro

    Quote Originally Posted by Hamish
    "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/libr...converter.aspx

    If you need help on using it I can give you a small example.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Re: Setting a property of a control in another project to a resource of the local project

    Oh dear.... another C# example.
    I'll see if I get it!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Re: Setting a property of a control in another project to a resource of the local project

    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?

  6. #6
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Re: Setting a property of a control in another project to a resource of the local pro

    Quote Originally Posted by Hamish
    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.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Location
    Zeist, The Netherlands
    Posts
    266

    Re: Setting a property of a control in another project to a resource of the local project

    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:

    Code:
    <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...
    I could of course just set the ColorMap property from the VUI_Main.Loaded event like this:

    Code:
    If MapCanvas.ColorMap Is Nothing Then MapCanvas.ColorMap = My.Resources.TempColorMap
    This works, but I'd prefer to do it all in the XAML.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width