【问题标题】:Control is white when setting DefaultStyleKey设置 DefaultStyleKey 时控件为白色
【发布时间】:2012-01-27 18:10:40
【问题描述】:

我似乎在任何地方都找不到遇到类似问题的人...

所以我有一个继承自 Button 的自定义控件。当我不应用默认样式时,它会加载通用 Button 样式,并且一切都很好。当我应用样式时,控件呈现为没有任何功能的纯白色正方形(单击不起作用等)。我确定是我的风格搞砸了,但是当我通过 XAML 中的键应用该风格时,它工作正常。

我的代码...

[TemplateVisualState(Name = "Normal", GroupName = "Common"),
TemplateVisualState(Name = "Disabled", GroupName = "Common"),
TemplateVisualState(Name = "Pressed", GroupName = "Common"),
TemplateVisualState(Name = "MouseOver", GroupName = "Common"),
TemplateVisualState(Name = "Alert", GroupName = "AlarmStates"),
TemplateVisualState(Name = "NoAlert", GroupName = "AlarmStates"),
TemplateVisualState(Name = "Warning", GroupName = "AlarmStates")]
public class AlarmButtonControl : Button
{
    /// <summary>
    /// Constructor
    /// </summary>
    public AlarmButtonControl()
    {
        this.DefaultStyleKey = typeof(AlarmButtonControl);
    }
}

XAML 样式是...

<ControlTemplate x:Key="StatusButton" >
        <Grid>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="Common">
                    <VisualStateGroup.Transitions>
                        <VisualTransition From="Pressed" GeneratedDuration="0:0:0.75" To="MouseOver"/>
                    </VisualStateGroup.Transitions>
                    <VisualState x:Name="Normal"/>
                    <VisualState x:Name="Disabled">
                        <Storyboard>
                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Highlight2" d:IsOptimized="True"/>
                            <ColorAnimation Duration="0" To="#BF000000" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Highlight2" d:IsOptimized="True"/>
                            <ColorAnimation Duration="0" To="#33666666" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Base" d:IsOptimized="True"/>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Pressed">
                        <Storyboard>
                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Highlight2" d:IsOptimized="True"/>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="MouseOver">
                        <Storyboard>
                            <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Highlight1" d:IsOptimized="True"/>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Viewbox Stretch="Fill">
                <Grid>
                    <Path x:Name="Base" 
                          StrokeThickness="1.0" 
                          Stroke="#ff000000" 
                          StrokeMiterLimit="1.0" 
                          Fill="{TemplateBinding Background}" Data="F1 M 99.500,99.500 L 0.500,99.500 L 0.500,0.500 L 99.500,0.500 L 99.500,99.500 Z"/>
                    <Rectangle x:Name="Highlight2" Fill="#3F000000" Margin="5" StrokeThickness="1.0" StrokeMiterLimit="1.0" Opacity="0"/>
                    <Path x:Name="Interior" Opacity="0.5" StrokeThickness="1.0" Stroke="#ff191919" StrokeMiterLimit="1.0" Data="F1 M 97.500,97.500 L 2.500,97.500 L 2.500,2.500 L 97.500,2.500 L 97.500,97.500 Z">
                        <Path.Fill>
                            <LinearGradientBrush MappingMode="Absolute" StartPoint="2.500,2.499" EndPoint="97.500,97.499">
                                <LinearGradientBrush.Transform>
                                    <MatrixTransform Matrix="1.000,0.000,-0.000,-1.000,0.000,100.000" />
                                </LinearGradientBrush.Transform>
                                <GradientStop Offset="0.00" Color="#3FFFFFFF"/>
                                <GradientStop Offset="0.151" Color="Transparent"/>
                                <GradientStop Offset="1.00" Color="#BFFFFFFF"/>
                                <GradientStop Color="#53FFFFFF" Offset="0.655"/>
                            </LinearGradientBrush>
                        </Path.Fill>
                    </Path>
                    <Path x:Name="LargeShader" Opacity="0.05" Fill="#ffffffff" Data="F1 M88.334,3.1789145E-07 L88.334,45.334 L0,45.334 C0,45.334 88.334,48.681 88.334,3.1789145E-07 z" Margin="6.333,49.333,5.333,5.284" UseLayoutRounding="False"/>
                    <Path x:Name="SmallShader" Opacity="0.05" Fill="#ffffffff" Data="F1 M74.333664,3.1789145E-07 L74.334,28.334332 L0,28.334332 C0,28.334332 60.000332,29.333 74.333664,3.1789145E-07 z" Height="28.339" Margin="20.333,0,5.333,5.328" UseLayoutRounding="False" VerticalAlignment="Bottom"/>
                    <Rectangle x:Name="Highlight1" Fill="#3FC0C0C0" Margin="5" StrokeThickness="1.0" StrokeMiterLimit="1.0" Opacity="0"/>
                </Grid>
            </Viewbox>
            <ContentPresenter x:Name="contentPresenter"
                              Height="{TemplateBinding Height}" 
                              Width="{TemplateBinding Width}" 
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                              Margin="5"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
        </Grid>
    </ControlTemplate>

任何帮助将不胜感激。

【问题讨论】:

    标签: silverlight xaml styling


    【解决方案1】:

    您没有包含Style,只是包含ControlTemplate。此外,对于默认样式,您应该只分配样式的 TargetType 属性。像下面这样的东西应该在themes\generic.xaml 文件中:

    <Style TargetType="local:AlarmButtonControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:AlarmButtonControl">
                    <!-- Your template contents here -->
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style> 
    

    【讨论】:

    • 天哪,我觉得自己很愚蠢,我确实有我原来的风格和二传手,但我的目标类型错误。我以前有一个 AlarmButton 用户控件,并在目标类型中引用它。现在我的动画出现了异常,所以我必须弄清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    相关资源
    最近更新 更多