如何在另一个项目中使用自定义WPF / XAML控件?

时间:2015-04-03 23:47:06

标签: c# wpf xaml custom-controls

我能够创建一个自定义WPF Glass Button来扩展Button类,并使用在资源字典中定义的Style中定义的ControlTemplate。

在容纳控件的项目中,它工作正常并且表现得如预期的那样。

当我尝试将控件放在另一个项目中时,我什么都没得到 - 什么也没出现。没有错误,但没有控制权。

我尝试使用UserControl构建控件,但它在触发器中的某些属性上出错 -

<UserControl.Triggers>
    <Trigger Property="IsPressed" Value="True"> <!--'IsPressed' member is not valid because it does not have a qualifying type name.-->
        <Setter Property="Visibility" TargetName="Glow" Value="Hidden"/> <!--The name "Glow" is not recognized."-->
        <Setter Property="Opacity" TargetName="Shine" Value="0.4"/> <!--The name "Shine" is not recognized."-->
        <Setter Property="Background" TargetName="Border" Value="#CC000000"/> <!--The name "Border" is not recognized."-->
    </Trigger>

这是整个XAML -

<UserControl x:Class="WPFTools.Controls.GB"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <Storyboard x:Key="GlowOn">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Glow">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="GlowOff">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Glow">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>
<Border x:Name="Border"
        BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
        Background="{Binding Background, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
        BorderThickness="4" CornerRadius="4">
    <Grid x:Name="Contents">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border x:Name="Glow" BorderThickness="1" Height="Auto" Grid.RowSpan="2" VerticalAlignment="Stretch" Width="Auto" CornerRadius="4" Opacity="0"
                              Background="{Binding GlowColor, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"/>
        <Viewbox Stretch="Uniform" Height="Auto" Grid.RowSpan="2" VerticalAlignment="Center" Width="Auto">
            <ContentPresenter x:Name="Content" HorizontalAlignment="Center"/>
        </Viewbox>
        <Border x:Name="Shine" BorderThickness="1,1,1,0" HorizontalAlignment="Stretch" Height="Auto" Margin="0" VerticalAlignment="Stretch" BorderBrush="{x:Null}" CornerRadius="4,4,0,0"
                                Background="{Binding ShineColor, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"/>
    </Grid>
</Border>
<UserControl.Triggers>
    <Trigger Property="IsPressed" Value="True">
        <Setter Property="Visibility" TargetName="Glow" Value="Hidden"/>
        <Setter Property="Opacity" TargetName="Shine" Value="0.4"/>
        <Setter Property="Background" TargetName="Border" Value="#CC000000"/>
    </Trigger>
    <Trigger Property="IsMouseOver" Value="True">
        <Trigger.ExitActions>
            <BeginStoryboard x:Name="GlowOff_BeginStoryboard" Storyboard="{StaticResource GlowOff}"/>
        </Trigger.ExitActions>
        <Trigger.EnterActions>
            <BeginStoryboard x:Name="GlowOn_BeginStoryboard" Storyboard="{StaticResource GlowOn}"/>
        </Trigger.EnterActions>
    </Trigger>
</UserControl.Triggers>

这是班级的代码 -

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WPFTools.Controls {
    /// <summary>
    /// Interaction logic for GB.xaml
    /// </summary>
    public partial class GB : Button {

        private static readonly DependencyProperty
    _ShineColor = DependencyProperty.Register( "ShineColor", typeof( Brush ), typeof( GlassButton ), new FrameworkPropertyMetadata(
        new LinearGradientBrush( ) {
            GradientStops = new GradientStopCollection( ) {
                            new GradientStop(Color.FromArgb(0x99, 0xFF, 0xFF, 0xFF), 0.0D),
                            new GradientStop(Color.FromArgb(0x33, 0xFF, 0xFF, 0xFF), 1.0D) },
            EndPoint = new Point( 0.5D, 1.0D ), StartPoint = new Point( 0.5D, 0.0D )
        } ) ), _GlowColor = DependencyProperty.Register( "GlowColor", typeof( Brush ), typeof( GlassButton ), new FrameworkPropertyMetadata(
        new RadialGradientBrush( ) {
            GradientStops = new GradientStopCollection( ){
                            new GradientStop( Color.FromArgb( 0xB2, 0x8D, 0xD8, 0xFF ), 0.0D),
                            new GradientStop( Color.FromArgb( 0x00, 0x8D, 0xD8, 0xFF ), 1.0D) },
            RelativeTransform = new TranslateTransform( ) { X = -0.25D, Y = 0.5D },
        } ) );

        public Brush ShineColor {
            get { return GetValue( GB._ShineColor ) as Brush; }
            set { SetValue( GB._ShineColor, value ); }
        }

        public Brush GlowColor {
            get { return GetValue( GB._GlowColor ) as Brush; }
            set { SetValue( GB._GlowColor, value ); }
        }

        public GB( ) {
            InitializeComponent( );
        }
    }
}

我显然在这里做错了 - 我只需要知道它是什么。

1 个答案:

答案 0 :(得分:1)

所以,事实证明,提供的链接给了我解决问题的答案 -

首先:当我创建包含自定义WPF控件的项目时,我错误地更改了在Themes文件夹中创建的Generic.xaml的名称。这显然是禁忌。

我犯的第二个错误是我没有修改assembly.cs中的ThemeInfo来表明需要资源。

解决这两个问题已经纠正了我的问题,按钮现在正在显示,我可以继续使用它进行测试。