【问题标题】:Passing specific parameters borderbrush to custom template将特定参数边框画笔传递给自定义模板
【发布时间】:2018-07-16 16:19:47
【问题描述】:

WPF,我有自定义模板,我想从我创建的 DependencyProperty 的 xaml 窗口将边框刷传递给这个模板

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

【问题讨论】:

    标签: c# wpf dependency-properties


    【解决方案1】:

    我发现了错误

    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 的错误

    【讨论】:

      猜你喜欢
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多