Results 1 to 9 of 9

Thread: WPF Brushes in an array?

  1. #1

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    WPF Brushes in an array?

    Hi all,

    I am working on a graph program, all works well but i have decided to add a feature to change the colour scheme. how this will work (if it can be done) is the relevant brush is obtained from an array. So herein lies the problem i am having. some of these schemes contain image brushes, some are drawing brushes, some solid brushes and some gradient brushes. There doesn't seem to be any type for an array that will allow any kind of brush (thought brushes would work).

    Anyone have a possible solution to this issue? maybe i can use strings and somehow use this?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  2. #2
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: WPF Brushes in an array?

    couldnt you use a dictionary instead? Something like this should work..

    Code:
           Dim di As New Dictionary(Of Int32, Brush)
            Dim br As New SolidColorBrush
            Dim lb As New LinearGradientBrush
            di.Add(0, br)
            di.Add(1, lb)

  3. #3

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: WPF Brushes in an array?

    ok i worked a lil on this and got this code which wont work
    Code:
            brushColour(1, 0) = New ImageBrush
            brushColour(1, 0).imagesource = New BitmapImage(New Uri("Images/red.jpg", UriKind.RelativeOrAbsolute))
            brushColour(1, 1) = New SolidColorBrush
            brushColour(1, 1) = Brushes.Red
            mainPage.Rectangle1.Fill = brushColour(1, 0)
    the program errors as soon as i try to use the array (filling it worked fine using type object). Looks like i'm going to have to create different arrays for each type (SolidColorBrush, ImageBrush, DrawingBrush, Linear GradientBrush etc)

    @bflosabre91 using a dictionary produces the same problem.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  4. #4
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: WPF Brushes in an array?

    im still not convinced that a dictionary wouldnt be the way to go for this. what is the error you get when you try to use a brush in the array or dictionary?

    this worked for me with no problems. i was able to set rect.Fill = dict(0) and rect.Fill = dict(1)

    Code:
        Private dict As New Dictionary(Of Int32, Brush)
    
    
            Dim im As New ImageBrush
            im.ImageSource = New BitmapImage(New Uri("C:/bubble.png", UriKind.RelativeOrAbsolute))
            dict.Add(0, im)
            Dim br As New SolidColorBrush
            br.Color = Colors.LightGreen
            dict.Add(1, br)

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: WPF Brushes in an array?

    Does it error here:
    brushColour(1, 0).imagesource = New BitmapImage(N....

    or here:
    mainPage.Rectangle1.Fill = brushColour(1, 0)


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: WPF Brushes in an array?

    Megalith - the difference between what you are trying to do and what bflosabre91 has done is in the creation of the object... you're trying to do that right into the array, and then set properties that are specific to the inherited type you created... which is why I asked where the error happens... if you create an array of Brush type... to add an ImageBrush (which should be inheriting form Brush) ... you need to create an ImageBrush object, set it's properties, then ADD THAT to the array. Same goes for a List(Of T) or Dictionary ... as long as your lists/arrays/what ever, are of Brush type, you should be able to add to them, but I don't think you can simply just do this:
    brushColour(1, 0).imagesource = New BitmapImage(New Uri("Images/red.jpg", UriKind.RelativeOrAbsolute))
    Because in order for that to work, your array would have to be an ImageBrush... which it isn't.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,380

    Re: WPF Brushes in an array?

    Seconded what techgnome says is true and I personally would also recommend Generic Containers over arrays. Either IList or IDictionary.

  8. #8

    Thread Starter
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: WPF Brushes in an array?

    ok guys thanks for the help i scrapped the array thing. @tg i got the error when i tried to change the rectangle fill. i declared the array as no type (presume this is type object?) anyway i updated to the dictionary idea (i'll look into IDictionary and IList too Dean) and had the sub in my globals module which caused an error (possibly circular???), when i moved it to within the same form as the listbox then it worked perfect and i can now change the colours as intended.

    I have one question, can i change the colour of a dictionary item after it is added? or will i need to remove the indexed item and replace it with a new brush of the type needed should i update the image somewhere. dont think i will need to but who knows :s

    will work with this over the coming days and let you know if i have any issues.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: WPF Brushes in an array?

    I have one question, can i change the colour of a dictionary item after it is added? or will i need to remove the indexed item and replace it with a new brush of the type needed
    I think that depends on whether or not the Brush type is a Class or a Structure (reference type or value type). If its a Structure (i.e. a value type) I think you will find that you cannot update an existing item in the collection - well you can do it, you wont get an exception or anything, but it will be as if you hadn't done it. If its a Class (i.e. a reference type) then there should be no problem updating existing items in the collection
    EDIT: Just to clarify, when I say "update an item in the collection" I mean modify a property of the item.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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