PDA

Click to See Complete Forum and Search --> : Accessing a XAML resource from a non UI class


chris128
Jul 1st, 2009, 01:55 PM
OK so I know that if I define a resource in XAML like this in a Window:

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

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:

<ValueConversion(GetType(ProtoChat.UserStates), GetType(LinearGradientBrush))> _
Public Class StatusToColourConverter : Implements IValueConverter

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
Dim BGBrush As New LinearGradientBrush
BGBrush.StartPoint = New Point(0.5, 0)
BGBrush.EndPoint = New Point(0.5, 1)
Dim Stops As New GradientStopCollection

Select Case DirectCast(value, ProtoChat.UserStates)
Case ProtoChat.UserStates.Online
Stops.Add(New GradientStop(Color.FromArgb(220, 226, 253, 234), 0.0))
Stops.Add(New GradientStop(Color.FromArgb(220, 183, 240, 200), 0.21))
Stops.Add(New GradientStop(Color.FromArgb(220, 110, 198, 135), 0.241))
Stops.Add(New GradientStop(Color.FromArgb(220, 156, 228, 178), 0.991))
Case ProtoChat.UserStates.Away
'Create gradient stops here
Case ProtoChat.UserStates.Busy
'Create gradient stops here
End Select

BGBrush.GradientStops = Stops
Return BGBrush
End Function

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
Return Nothing
End Function
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

chris128
Jul 1st, 2009, 02:05 PM
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:

Stops.Add(New GradientStop(DirectCast(ColorConverter.ConvertFromString("#CCE2FDEA"), Color), 0.0))
Stops.Add(New GradientStop(DirectCast(ColorConverter.ConvertFromString("#CCB7F0C8"), Color), 0.21))
Stops.Add(New GradientStop(DirectCast(ColorConverter.ConvertFromString("#CC6EC687"), Color), 0.241))
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

DeanMc
Jul 1st, 2009, 02:19 PM
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?

chris128
Jul 1st, 2009, 03:13 PM
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...

Arrow_Raider
Jul 4th, 2009, 06:57 PM
You could pass the brush in as a converter parameter.
<Border Background="{Binding Blah, Converter={StaticResource StatusToColourConverter}, ConverterParameter={StaticResource MyBrush}}" />

Then in your converter code, you just need to cast parameter to Brush.

chris128
Jul 5th, 2009, 04:42 AM
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

Arrow_Raider
Jul 5th, 2009, 05:58 AM
You can use the x:Array markup to create an array of objects and pass this array in as the parameter.
<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).

chris128
Jul 5th, 2009, 06:54 AM
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 :(