簡體   English   中英

如何在另一個項目中使用自定義WPF / XAML控件?

[英]How do I use a custom WPF/XAML control in another project?

我能夠創建一個擴展Button類的自定義WPF Glass 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( );
        }
    }
}

我顯然在這里做錯了-我只需要知道它是什么。

因此,事實證明,提供的鏈接給了我解決問題的答案-

首先:創建包含自定義WPF控件的項目時,我犯了一個錯誤,即更改了在Themes文件夾中創建的Generic.xaml的名稱。 顯然這是一個禁忌。

我犯的第二個錯誤是我沒有修改assembly.cs中的ThemeInfo來表明需要資源。

解決這兩個問題已解決了我的問題,該按鈕現在應有的樣子出現了,我可以繼續對其進行測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM