将特定参数borderbrush传递到自定义模板

时间:2018-07-16 16:19:47

标签: c# wpf dependency-properties

WPF,我有自定义模板,我想从我创建的DependencyProperty的xaml窗口中将borderbrush传递给此模板。

 public class PassParamBrush
{
    public static DependencyProperty BorderBrushProperty = DependencyProperty.RegisterAttached
    (
     "BorderBrush",
     typeof(SolidColorBrush),
     typeof(PassParamBrush),
     new PropertyMetadata(null)
    );
    public static SolidColorBrush GetBorderBrush(DependencyObject target)
    {
        return (SolidColorBrush)target.GetValue(BorderBrushProperty);
    }
    public static void SetBorderBrush(DependencyObject target, ImageSource value)
    {
        target.SetValue(BorderBrushProperty, value);
    }


}

在UI中,我以这种方式称呼

 xmlns:localOne="clr-namespace:Turk_Common_WPF_Style.Style.WindowsStyle"
 <Button   localOne:PassParamBrush.BorderBrush="#aadbff"  Width="30" Height="30"  Style="{DynamicResource btnLogout}" />

这里是模板

 <!--Buton With Image background-->
<Style  x:Key="btnLogout" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Border CornerRadius="150" BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:PassParamToTemplter.PassParamBrush.BorderBrush)}" BorderThickness="5">
                    <!--<Image   Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:PassParamToTemplter.ImageSource)}" 
                             Margin="2" Stretch="Fill" RenderOptions.BitmapScalingMode="HighQuality"/>-->
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect ShadowDepth="0" Color="Gray" Opacity="1" BlurRadius="25" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>

                    <Trigger Property="Button.IsPressed" Value="True">
                        <Setter Property="Margin" Value="7,7,7,0" />
                        <Setter Property="Effect">
                            <Setter.Value>
                                <DropShadowEffect ShadowDepth="0" Color="#E8AA6E" Opacity="1" BlurRadius="25" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但是当我运行代码并向我显示此错误时仍无法正常工作

** + InnerException {“未找到适合完成此操作的映像组件。”} System.Exception {System.NotSupportedException} **

1 个答案:

答案 0 :(得分:0)

我发现了错误

public class PassParamBrush{
public static DependencyProperty BorderBrushProperty = DependencyProperty.RegisterAttached
(
 "BorderBrush",
 typeof(SolidColorBrush),
 typeof(PassParamBrush),
 new PropertyMetadata(null)
);
public static SolidColorBrush GetBorderBrush(DependencyObject target)
{
    return (SolidColorBrush)target.GetValue(BorderBrushProperty);
}
public static void SetBorderBrush(DependencyObject target, ImageSource value)
{
    target.SetValue(BorderBrushProperty, value);
}

}

我应该将SetBorderBrush中的格式从ImageSource更改为SolidColorBrush的错误