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