Windows应用商店应用 - XAML C# - 自定义控件未设置DependencyProperty

时间:2014-07-21 14:33:36

标签: c# xaml windows-store-apps dependency-properties

我编写了自定义控件,这实际上是AppBarButton的扩展,但我的XAML声明似乎没有在Symbol {{1}中正确设置SymbolIcon }}

这是我的DependencyProperty

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

结果

Buttons

我确定我错过了一些简单的东西,但无法发现它。

1 个答案:

答案 0 :(得分:1)

发现问题,我应该使用Symbol设置SymbolIcon的{​​{1}}属性。所以它应该返回DependencyProperty枚举。

Symbol