Results 1 to 8 of 8

Thread: Accessing a XAML resource from a non UI class

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Accessing a XAML resource from a non UI class

    OK so I know that if I define a resource in XAML like this in a Window:

    Code:
    <Window.Resources>
         <SolidColorBrush x:Key="MyBrush" Color="Black" />
    </Window.Resources>
    then its easy for me to grab that resource from the code behind of that window, like this:
    vb.net Code:
    1. DirectCast(FindResource("MyBrush"),SolidColorBrush)
    or whatever, thats just off the top of my head so might not be 100% accurate but you get the idea.

    Thats all well and good, but how can I access that resource from a class other than the code behind class for that window?
    The situation is this - I am using a Converter (first time I have tried to use one) to set the background colour of a border in a listbox item based on the value of a property of the item that the listbox item is bound to... if that makes sense.
    Here is my converter class:
    vb.net Code:
    1. <ValueConversion(GetType(ProtoChat.UserStates), GetType(LinearGradientBrush))> _
    2. Public Class StatusToColourConverter : Implements IValueConverter
    3.  
    4.     Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    5.         Dim BGBrush As New LinearGradientBrush
    6.         BGBrush.StartPoint = New Point(0.5, 0)
    7.         BGBrush.EndPoint = New Point(0.5, 1)
    8.         Dim Stops As New GradientStopCollection
    9.  
    10.         Select Case DirectCast(value, ProtoChat.UserStates)
    11.             Case ProtoChat.UserStates.Online
    12.                 Stops.Add(New GradientStop(Color.FromArgb(220, 226, 253, 234), 0.0))
    13.                 Stops.Add(New GradientStop(Color.FromArgb(220, 183, 240, 200), 0.21))
    14.                 Stops.Add(New GradientStop(Color.FromArgb(220, 110, 198, 135), 0.241))
    15.                 Stops.Add(New GradientStop(Color.FromArgb(220, 156, 228, 178), 0.991))
    16.             Case ProtoChat.UserStates.Away
    17.                 'Create gradient stops here
    18.             Case ProtoChat.UserStates.Busy
    19.                 'Create gradient stops here
    20.         End Select
    21.  
    22.         BGBrush.GradientStops = Stops
    23.         Return BGBrush
    24.     End Function
    25.  
    26.     Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
    27.         Return Nothing
    28.     End Function
    29. End Class

    As you can see, it isnt exactly pretty and its not easy to work out the values for each gradient stop, so im hoping I can just define the brush as a resource in XAML (as its much easier to do in XAML, you just pass it a string of #CC6EC687 or whatever and it converts it to the correct brush) and then access that brush from my Converter. However I am open to other suggestions as there may be a better way of doing this! (would it be possible to use the same converter that XAML uses to convert the string into a colour but from code?)

    Cheers
    Chris
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  2. #2

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Accessing a XAML resource from a non UI class

    Ah I just found that I can use the same converter XAML uses to convert the colours from strings to Media.Color (although for some reason, the ColorConverter.ConvertFromString function returns a type of Object and not Media.Colour)

    So now I can do this in each Select Case statement:

    vb Code:
    1. Stops.Add(New GradientStop(DirectCast(ColorConverter.ConvertFromString("#CCE2FDEA"), Color), 0.0))
    2.                 Stops.Add(New GradientStop(DirectCast(ColorConverter.ConvertFromString("#CCB7F0C8"), Color), 0.21))
    3.                 Stops.Add(New GradientStop(DirectCast(ColorConverter.ConvertFromString("#CC6EC687"), Color), 0.241))
    4.                 Stops.Add(New GradientStop(DirectCast(ColorConverter.ConvertFromString("#CC9CE4B2"), Color), 0.991))

    so thats better, but still not ideal. I would still rather just be able to find an existing brush resource and use that instead of having to create a brush and each gradient stop etc
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


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

    Re: Accessing a XAML resource from a non UI class

    Ok I have no idea what that is all about I really need to catch up but it brinbgs up the point is there know way to share resources better than this?

  4. #4

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Accessing a XAML resource from a non UI class

    Its easy enough sharing resources in XAML but accessing XAML resources from code seems to always be a bit of a chore. In this case because I am wanting to access the XAML resource from a class totally separate to the Window where the resource is defined it seems to be impossible because you cant use FindResource or TryFindResource...
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  5. #5
    Hyperactive Member Arrow_Raider's Avatar
    Join Date
    Dec 2001
    Location
    AVR Lovers Club
    Posts
    423

    Re: Accessing a XAML resource from a non UI class

    You could pass the brush in as a converter parameter.
    Code:
    <Border Background="{Binding Blah, Converter={StaticResource StatusToColourConverter}, ConverterParameter={StaticResource MyBrush}}" />
    Then in your converter code, you just need to cast parameter to Brush.
    My monkey wearing the fedora points and laughs at you.

  6. #6

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Accessing a XAML resource from a non UI class

    The thing is though I need to access a few different brushes so could I pass say 3 brushes in as parameters or does that only work for 1 object?

    Thanks
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  7. #7
    Hyperactive Member Arrow_Raider's Avatar
    Join Date
    Dec 2001
    Location
    AVR Lovers Club
    Posts
    423

    Re: Accessing a XAML resource from a non UI class

    You can use the x:Array markup to create an array of objects and pass this array in as the parameter.
    Code:
    <Window.Resources>
    	<ResourceDictionary>
    		<x:Array Type="{x:Type Brush}" x:Key="MyBrushes">
    			<SolidColorBrush Color="Black" />
    			<SolidColorBrush Color="Red" />
    		</x:Array>
    	</ResourceDictionary>
    </Window.Resources>
    Then in your converter, cast parameter as Brush(). (Or however you cast something as an array of Brushes in VB.net).
    My monkey wearing the fedora points and laughs at you.

  8. #8

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Accessing a XAML resource from a non UI class

    Ah I see I'll give that a go then thanks. Seems odd that there is no easier way to just access something defined in XAML though
    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