Styling_Buttons_FlatIconButton_3_162224436' TargetType 与元素类型'Styling_Buttons_FlatIconButton_4_162224436'

问题描述

我有一个包含 2 个项目的解决方案。

  1. 样式(WPF 控件库、.NET 4.7.1、x64)
  2. Styling.Test(WPF 应用程序、.NET 4.7.1、x64)

Styling 项目有一个自定义控件,它命名为 FlatIconButton 控件派生自 Button 类本身,以及该图标按钮的正确样式。

Styling.Test 项目引用了 Styling,而 App.xml 具有 MergedDictionary 以继承包含在 Styling dll 中的样式。

一旦我将控件放到 MainWindow 中,设计器就会抛出 'Styling_Buttons_FlatIconButton_3_162224436' TargetType does not match type of element 'Styling_Buttons_FlatIconButton_4_162224436'. 错误。 但是当我运行该项目时,一切正常。

无论我做什么,我都做不到。

错误图片:https://imgur.com/a/jeEJPqc

样式/按钮/FlatIconButton.cs

using FontAwesome5;
namespace Styling.Buttons
{
    public class FlatIconButton : Button
    {

        public EFontAwesomeIcon Icon
        {
            get { return (EFontAwesomeIcon)GetValue(IconProperty); }
            set { SetValue(IconProperty,value); }
        }

        public ColorModes ColorMode
        {
            get { return (ColorModes)GetValue(ColorModeProperty); }
            set { SetValue(ColorModeProperty,value); }
        }

        public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon",typeof(EFontAwesomeIcon),typeof(FlatIconButton),new FrameworkPropertyMetadata(EFontAwesomeIcon.Regular_User,FrameworkPropertyMetadataOptions.AffectsRender));
        public static readonly DependencyProperty ColorModeProperty = DependencyProperty.Register("ColorMode",typeof(ColorModes),new FrameworkPropertyMetadata(ColorModes.Secondary,FrameworkPropertyMetadataOptions.AffectsRender));
    }
}

样式/模板/按钮.xaml

<Style x:Key="BaseButtonStyle" TargetType="{x:Type buttons:FlatIconButton}">
    <Setter Property="Background" Value="Red"/>
</Style>


<Style x:Key="FlatIconButtonStyle" 
       BasedOn="{StaticResource BaseButtonStyle}"
       TargetType="{x:Type buttons:FlatIconButton}">
    <Setter Property="Padding" Value="4,0" />
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Template" Value="{DynamicResource FlatIconButtonTemplate}"/>
</Style>

<ControlTemplate x:Key="FlatIconButtonTemplate" TargetType="{x:Type buttons:FlatIconButton}">
    <Border Padding="{TemplateBinding Padding}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            CornerRadius="2"
            SnapsToDevicePixels="True">
        <fa5:SvgAwesome Icon="{Binding Path=Icon,Mode=OneWay,RelativeSource={RelativeSource TemplatedParent}}"
                             Margin="{TemplateBinding Padding}"
                      Foreground="{TemplateBinding Foreground}"
                            VerticalAlignment="Center"
                      HorizontalAlignment="Center"
                      SnapsToDevicePixels="True"
                        Width="10"
                        Height="10"
                        />
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.5" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Styling.Test/MainWindow.xaml

<Window x:Class="Styling.Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:buttons="clr-namespace:Styling.Buttons;assembly=Styling"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="10">
        <buttons:FlatIconButton
            Style="{StaticResource FlatIconButtonStyle}"
            Icon="Solid_ChevronDown"
            VerticalAlignment="Top"
            HorizontalAlignment="Left"
                Margin="5"
            Padding="10,5 "
            />
    </Grid>
</Window>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)