2015-04-03 32 views
1

我能夠創建一個擴展Button類的自定義WPF Glass Button,並使用在資源字典中定義的樣式中定義的ControlTemplate。如何在另一個項目中使用自定義WPF/XAML控件?

在容納控件的項目中,它工作正常並且按預期運行。

當我嘗試將控件放置在另一個項目中時,我什麼也沒得到 - 什麼都沒有出現。沒有錯誤,但沒有控制權。

我試圖建立一個用戶控件控制,但對一些觸發屬性的錯誤 -

<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(); 
     } 
    } 
} 

我明顯在這裏做錯了事 - 我只需要知道它是什麼。

+0

'我明顯在這裏做錯了事 - 我只需要知道它是什麼「 - 基本上所有的東西。開始閱讀[這裏](https://msdn.microsoft.com/en-us/library/ms745025(v = vs.110).aspx)。 – 2015-04-03 23:54:21

+0

@HighCore 不完全 - 謝謝你的鏈接;我做錯了什麼是我已經將資源字典XAML的名稱從Generic更改爲GlassButton,並且我沒有修改程序集中的ThemeInfo。處理這兩點,並妥善保存控制模板已經很好地解決了這個問題。如果你沒有提供這個鏈接,我將無法解決這個問題,所以,謝謝你。 – Will 2015-04-04 19:37:26

+0

我瞭解新項目也是WPF,風格,你在哪裏定義它?,如果它在另一個項目中,它不會在新項目中被讀取。您需要將新資源字典複製到新項目並引用它。 – 2015-04-04 07:24:03

回答

1

所以,原來提供的鏈接給我的回答我的問題 -

首先:當我創建的房子我的自定義WPF控件的項目,我做了改變名稱的錯誤在Themes文件夾中創建的Generic.xaml。這顯然是一個禁忌。

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

解決這兩個問題已經糾正了我的問題,並且按鈕現在應該出現了,我可以繼續使用它進行測試。

相關問題