自定义控件在wpf中不起作用

时间:2015-11-05 19:25:07

标签: c# .net wpf xaml silverlight

所以现在我们正在从silverlight变为wpf。为silverlight实现的自定义控件,我试图将它们复制为wpf。问题是控件在wpf中不可见。

以下是控件的实现:

xaml文件(c = CcsFeedbackLED.xaml):

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:controlls"
xmlns:vsm="clr-namespace:System.Windows;assembly=PresentationFramework">

<Style TargetType="local:CcsFeedbackLED">
    <Setter Property="EnabledColor" Value="Gray" />
    <Setter Property="IsEnabled" Value="True" />
    <Setter Property="Visibility" Value="Visible" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:CcsFeedbackLED">
                <Grid>
                    <vsm:VisualStateManager.VisualStateGroups>
                        <vsm:VisualStateGroup x:Name="CommonStates">

                            <vsm:VisualState x:Name="Normal">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantOverlay"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.50"
                                        Storyboard.TargetProperty="(Rectangle.Fill).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#40000000" 
                                            KeyTime="00:00:00.50"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantOverlay"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.50"
                                        Storyboard.TargetProperty="(Rectangle.Fill).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#5E000000" 
                                            KeyTime="00:00:00.50"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantOverlay"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.50"
                                        Storyboard.TargetProperty="(Rectangle.Fill).(LinearGradientBrush.GradientStops)[2].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#00000000" 
                                            KeyTime="00:00:00.50"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantGloss"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.50"
                                        Storyboard.TargetProperty="(Ellipse.Fill).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#48FFFFFF" 
                                            KeyTime="00:00:00.50"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantGloss"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.50"
                                        Storyboard.TargetProperty="(Ellipse.Fill).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#CEFFFFFF" 
                                            KeyTime="00:00:00.50"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                            <vsm:VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantOverlay"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.1"
                                        Storyboard.TargetProperty="(Rectangle.Fill).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#B0000000" 
                                            KeyTime="00:00:01"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantOverlay"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.1"
                                        Storyboard.TargetProperty="(Rectangle.Fill).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#CE000000" 
                                            KeyTime="00:00:00.1"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantOverlay"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.1"
                                        Storyboard.TargetProperty="(Rectangle.Fill).(LinearGradientBrush.GradientStops)[2].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#70000000" 
                                            KeyTime="00:00:00.1"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantGloss"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.1"
                                        Storyboard.TargetProperty="(Ellipse.Fill).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#48FFFFFF" 
                                            KeyTime="00:00:00.1"/>
                                    </ColorAnimationUsingKeyFrames>
                                    <ColorAnimationUsingKeyFrames 
                                        Storyboard.TargetName="GradiantGloss"
                                        BeginTime="00:00:00"
                                        Duration="00:00:00.1"
                                        Storyboard.TargetProperty="(Ellipse.Fill).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)"
                                        FillBehavior="HoldEnd">
                                        <LinearColorKeyFrame 
                                            Value="#CEFFFFFF" 
                                            KeyTime="00:00:00.1"/>
                                    </ColorAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>

                    <ScrollViewer 
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        VerticalScrollBarVisibility="Hidden" 
                        IsTabStop="False"
                        Padding="0" 
                        Background="{TemplateBinding EnabledColor}">
                        <Grid Background="Transparent">
                            <Rectangle 
                                x:Name="GradiantOverlay"  
                                HorizontalAlignment="Stretch" 
                                VerticalAlignment="Stretch">
                                <Rectangle.Fill>
                                    <LinearGradientBrush StartPoint="0,1" EndPoint="0,0">
                                        <GradientStop Color="#40000000" Offset="1" />
                                        <GradientStop Color="#5E000000" Offset="0.90" />
                                        <GradientStop Color="#00000000" Offset="0" />
                                    </LinearGradientBrush>
                                </Rectangle.Fill>
                            </Rectangle>
                            <Ellipse 
                                x:Name="GradiantGloss"
                                RenderTransformOrigin="0.5,0"
                                Width="Auto"
                                HorizontalAlignment="Stretch"
                                VerticalAlignment="Stretch">
                                <Ellipse.Fill>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                        <GradientStop Color="#78FFFFFF" Offset="1" />
                                        <GradientStop Color="#EEFFFFFF" Offset="0" />
                                    </LinearGradientBrush>
                                </Ellipse.Fill>
                                <Ellipse.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform ScaleX="1.75" ScaleY="0.65" />
                                    </TransformGroup>
                                </Ellipse.RenderTransform>
                            </Ellipse>
                        </Grid>
                    </ScrollViewer>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

File(CscsFeedbackLED.cs)背后的代码:

    using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace controlls
{
    using Harris.BCD.Toolkit.Silverlight.Controls.Utilities;

    /// <summary>
    ///     A simple LED Control with a customizable color
    /// </summary>
    [TemplateVisualState(Name = VisualStateInfo.StateNormal, GroupName = VisualStateInfo.GroupCommonStates)]
    [TemplateVisualState(Name = VisualStateInfo.StateDisabled, GroupName = VisualStateInfo.GroupCommonStates)]
    [ImplementsControlEvents]
    [InheiritedControlEventsAction(typeof(Control), "IsEnabled")]
    public class CcsFeedbackLED : Control, INotifyPropertyChanged
    {
        #region Dependency Property Definitions

        /// <summary>
        ///     Color Dependency Property
        /// </summary>
        public static DependencyProperty EnabledColorProperty = DependencyProperty.Register(
            "EnabledColor", typeof(Brush), typeof(CcsFeedbackLED),
            new PropertyMetadata(new PropertyChangedCallback(CcsFeedbackLED.OnColorPropertyChanged)));

        #endregion
        #region Data Properties

        /// <summary>
        ///     The color of the LED, specified in Hexadecimal form:
        ///     Format: #DDRRGGBB
        ///         DD - Opacity (00-FF)
        ///         RR - Red Channel (00-FF)
        ///         GG - Green Channel (00-FF)
        ///         BB - Blue Channel (00-FF)
        /// </summary>
        [ControlEventsAction]
        public Brush EnabledColor
        {
            get { return (Brush)GetValue(CcsFeedbackLED.EnabledColorProperty); }
            set { SetValue(CcsFeedbackLED.EnabledColorProperty, value); }
        }

        #endregion
        #region Object Construction

        /// <summary>
        ///     Default object constructor
        /// </summary>
        public CcsFeedbackLED()
            : base()
        {
            this.DefaultStyleKey = typeof(CcsFeedbackLED);      // Register the Style Key

            this.IsEnabledChanged += delegate(object sender, DependencyPropertyChangedEventArgs e)
            {
                this.UpdateVisualState();
            };
        }


        /// <summary>
        ///     Registers named child controls when a template is applied.
        ///     Also responsible for registering data-contexts for data bindings.
        ///     *** DO NOT INVOKE DIRECTLY ***
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.UpdateVisualState();
        }

        #endregion
        #region Object Methods

        /// <summary>
        ///     Updates the Visual State of the Control
        /// </summary>
        protected virtual void UpdateVisualState()
        {
            VisualStateManager.GoToState(this, (this.IsEnabled) ? VisualStateInfo.StateNormal : VisualStateInfo.StateDisabled, true);
        }

        #endregion
        #region Control Events

        /// <summary>
        ///     Generic Property Change event, fired whenever a dependency property is changed
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        ///     Notify when a property changes
        /// </summary>
        /// <param name="propertyName">The Name of the Property Changed</param>
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
        #region Control Event Handling

        /// <summary>
        ///     Event fired whenever the color property changes
        ///     ** DO NOT INVOKE DIRECTLY **
        /// </summary>
        /// <param name="e"></param>
        protected void OnColorChange(DependencyPropertyChangedEventArgs e)
        {
            this.NotifyPropertyChanged("EnabledColor");
        }

        #endregion
        #region Static Property Dependency Handling

        private static void OnColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CcsFeedbackLED objChange = d as CcsFeedbackLED;

            if (d == null)
                return;

            objChange.OnColorChange(e);
        }

        #endregion
    }
}

我试着通过引用上面的项目在wpf应用程序中使用它,如下所示:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:he="clr-namespace:controlls;assembly=controlls"
        Title="MainWindow" HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch">

    <Grid>
        <he:CcsFeedbackLED

                                            Margin="-44,-96,10,31"
                                            EnabledColor="#FF00FF00"
                                            IsEnabled="True" />
    </Grid>
</Window>

但是当我运行应用程序时,我无法看到控件? 这有一些quickFix吗?这个问题很可能是将控件直接从silverlight复制到wpf

的结果

0 个答案:

没有答案