所以我有一个应用程序,我可以配置我希望应用程序看起来像app.config的方式。该应用程序包含一个用户控件,我打电话给#34; Callout"用于呼叫下一个客户。我希望在下一个客户属性更改时为此用户控件的背景设置动画。
我知道怎么做,我唯一的问题是我在转换器上接收了它的背景配置并将它们转换为Solid,LinearGradient或RadialGradient Brush。为了给渐变停止设置动画,我需要注册他们的名字,而我无法在转换器中访问该方法。
如何在转换器上实现此效果?我怎么能在其中注册名字?
以下是相关的代码:
位于app.config中的部分configSection:
<callout key="PacientCallout"
title="Paciente"
placeholder="N/D"
border="#FFFFC20E">
<titleFont family="/Clinical.Resources;Component/Fonts/#Dax-Regular"
size="40"
isCustom="true">
<colors type="Solid">
<color key="TitleColor" code="White" />
</colors>
</titleFont>
<textFont family="/Clinical.Resources;Component/Fonts/#Dax-Regular"
size="30"
isCustom="true">
<colors type="Solid">
<color key="TextColor" code="White" />
</colors>
</textFont>
<!--Possible values for type are Solid, LinearGradient or RadialGradient-->
<background type="LinearGradient">
<color key="StarBlue" code="#FF2A398C" />
<color key="EndBlue" code="#FF0D174F" />
</background>
</callout>
我正在放置所有用户控件元素的边框:
<Border CornerRadius="30,0,0,30"
Margin="0,15,0,15"
BorderBrush="{Binding Border,
Converter={StaticResource StringToBrushConverter},
Mode=TwoWay,
PresentationTraceSources.TraceLevel=High}"
Background="{Binding Background,
Converter={StaticResource StringToGradientStopConverter},
Mode=TwoWay,
PresentationTraceSources.TraceLevel=High}"
BorderThickness="2"
Width="400"
Height="150">
<Border.Triggers>
</Border.Triggers>
<Border.BitmapEffect>
<DropShadowBitmapEffect Color="Black"
Direction="225"
ShadowDepth="6"
Opacity="0.5" />
</Border.BitmapEffect>
<!-- More User Control stuff in here -->
</Border>
背景转换器配置为刷子:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
return null;
var gradient = value as Clinical.Core.Configuration.ColorCollection;
double offset = 0;
Brush brush;
switch (gradient.Type)
{
case BackgroundType.Solid:
brush = (SolidColorBrush)new BrushConverter().ConvertFrom(gradient[0].Code);
break;
case BackgroundType.LinearGradient:
LinearGradientBrush linearGradient = new LinearGradientBrush();
linearGradient.StartPoint = new Point(0.5, 0);
linearGradient.EndPoint = new Point(0.5, 1);
linearGradient.MappingMode = BrushMappingMode.RelativeToBoundingBox;
foreach (Clinical.Core.Configuration.ColorElement color in gradient)
{
var colorCode = (Color)ColorConverter.ConvertFromString(color.Code);
var gradientStop = new GradientStop(colorCode, offset);
linearGradient.GradientStops.Add(gradientStop);
offset += 1d / ((double)gradient.Count-1);
}
brush = linearGradient;
break;
case BackgroundType.RadialGradient:
RadialGradientBrush radialGradient = new RadialGradientBrush();
radialGradient.GradientOrigin = new Point(0.75,0.15);
radialGradient.MappingMode = BrushMappingMode.RelativeToBoundingBox;
foreach (Clinical.Core.Configuration.ColorElement color in gradient)
{
var colorCode = (Color)ColorConverter.ConvertFromString(color.Code);
var gradientStop = new GradientStop(colorCode, offset);
radialGradient.GradientStops.Add(gradientStop);
offset += 1d / ((double)gradient.Count-1);
}
brush = radialGradient;
break;
default:
brush = null;
break;
}
return brush;
}
答案 0 :(得分:-1)
在您的情况下,最好的解决方案是使用MultiBinding而不是Binding,如下所示:
<Border.BorderBrush>
<MultiBinding Converter="Converter={StaticResource StringToBrushConverter}">
<MultiBinding.Bindings>
<Binding PresentationTraceSources.TraceLevel="High"/>
<Binding Path="." ElementName="border" />
</MultiBinding.Bindings>
</MultiBinding>
</Border.BorderBrush>
通过这种方式,您可以将多个对象传递到转换器中。例如Path =“。”允许您传递任何控件或对象的实例。另请注意,您必须为MultiBinding实现IMultiValueConverter,而不是IValueConverter。希望这有助于你。