【发布时间】:2014-07-21 14:33:35
【问题描述】:
我编写了自定义控件,它实际上是 AppBarButton 的扩展,但我的 XAML 声明似乎没有在 SymbolIcon DependencyProperty 中正确设置 Symbol。
这是我的Style
<Style TargetType="local:NcButton">
<Setter Property="Foreground" Value="{ThemeResource NcThirdColour}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:NcButton">
<Grid Background="Transparent" x:Name="RootGrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="Ellipse">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource AppBarItemPointerOverBackgroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="Content">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource AppBarItemPointerOverForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="Ellipse">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource NcThirdColour}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="Ellipse">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource NcThirdColour}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="Content">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource AppBarItemPressedForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualWhite"/>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualBlack"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="PointerFocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid
Height="41"
Width="41">
<Ellipse
x:Name="Ellipse"
Fill="{ThemeResource AppBarItemBackgroundThemeBrush}"
Stroke="{TemplateBinding Foreground}"
StrokeThickness="2"
UseLayoutRounding="False" />
<ContentPresenter
x:Name="Content"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<SymbolIcon Symbol="{TemplateBinding Symbol}"/>
</ContentPresenter>
</Grid>
<Rectangle x:Name="FocusVisualWhite" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{ThemeResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1"/>
<Rectangle x:Name="FocusVisualBlack" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="0.5" StrokeEndLineCap="Square" Stroke="{ThemeResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我的自定义控件
public sealed class NcButton : Button
{
public NcButton()
{
this.DefaultStyleKey = typeof(NcButton);
}
// you can get help for these properties using the propdp code snippet in C# and Visual Basic
public SymbolIcon Symbol
{
get { return (SymbolIcon)GetValue(SymbolProperty); }
set { SetValue(SymbolProperty, value); }
}
// Using a DependencyProperty as the backing store for ImagePath. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SymbolProperty =
DependencyProperty.Register("Symbol", typeof(SymbolIcon), typeof(NcButton), new PropertyMetadata(new SymbolIcon(Windows.UI.Xaml.Controls.Symbol.Message)));
}
我的声明
<StackPanel Orientation="Horizontal" Grid.Column="1">
<controls:NcButton Symbol="Message"/>
<controls:NcButton Symbol="ZeroBars" Foreground="{ThemeResource NcTaskRed}"/>
<controls:NcButton Symbol="FourBars" Foreground="{ThemeResource NcTaskGreen}"/>
</StackPanel>
结果
我确定我错过了一些简单的东西,但无法发现它。
【问题讨论】:
标签: c# xaml windows-store-apps dependency-properties