Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the necessity to change the brush on the rectangle on the change of double value.

The problem is that sometimes the brush is value and sometime is dynamic resource.

Whit "Application.Current.Resources" it's possible take the actual value but i have the necessity to bind the resource to control (for esemple to obj.SetResourceReference). It's possible?

What I have tried:

VB.NET
Private Class ColorConv
    Implements IValueConverter

        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Dim s As Brush

            If value =0 Then
                s = Brushes.red
            Else
                 s = Application.Current.Resources("myResources")
            End If

            Return s
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Return 0
    End Function
End Class
Posted
Updated 18-May-16 2:13am

Sounds like a job for a DataTrigger[^]:
XML
<Rectangle>
    <Rectangle.Style>
        <Style>
            <Setter Property="Fill" Value="{DynamicResource myResources}" />
            
            <Style.Triggers>
                <DataTrigger Binding="{Binding myValue}" Value="0">
                    <Setter Property="Fill" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Rectangle.Style>
</Rectangle>

Trigger, DataTrigger & EventTrigger[^]
Triggers in WPF[^]
 
Share this answer
 
With bind style works

C#
BindingOperations.SetBinding(myLed, Rectangle.StyleProperty, myBinding)


When create the style if hacve the resurce use DynamicResourceExtension


Dim dynamicResource = New DynamicResourceExtension(mybrush)
GetStyle.Setters.Add(New Setter(Rectangle.FillProperty, dynamicResource))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900