有时可以绑定某些时间值的资源吗? [英] Is possible bind sometime value sometime resource?
问题描述
我有必要在更改双值时更改矩形上的画笔。
问题是有时刷子是有价值的,有时是动态的资源。
WhitApplication.Current.Resources它可以取实际值,但我有必要将资源绑定到控件上(对于obj.SetResourceReference来说) )。这可能吗?
我尝试过:
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:
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
推荐答案
听起来像个工作对于 DataTrigger [ ^ ]:
Sounds like a job for a DataTrigger[^]:
<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 [ ^ ]
WPF中的触发器 [ ^ ]
with bind风格作品
With bind style works
BindingOperations.SetBinding(myLed, Rectangle.StyleProperty, myBinding)
创建样式时如果出现重新使用DynamicResourceExtension
When create the style if hacve the resurce use DynamicResourceExtension
Dim dynamicResource = New DynamicResourceExtension(mybrush)
GetStyle.Setters.Add(New Setter(Rectangle.FillProperty, dynamicResource))
这篇关于有时可以绑定某些时间值的资源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!