Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# basedOn是否也继承了可视状态?_C#_Wpf_Button - Fatal编程技术网

C# basedOn是否也继承了可视状态?

C# basedOn是否也继承了可视状态?,c#,wpf,button,C#,Wpf,Button,我正在使用自定义模板创建多个按钮。每个按钮都绑定到不同的缩略图,但我希望它们都具有相同的视觉状态。我创建了customButtonStyle样式,每个按钮都将继承此样式。但是,视觉状态似乎不是从customButtonStyle继承的 你知道为什么它不会继承视觉状态吗?如何将每个按钮设置为自己的缩略图 提前谢谢 下面是我的代码: <Style x:Key="customButtonStyle" TargetType="Button"> <Setter Property=

我正在使用自定义模板创建多个按钮。每个按钮都绑定到不同的缩略图,但我希望它们都具有相同的视觉状态。我创建了customButtonStyle样式,每个按钮都将继承此样式。但是,视觉状态似乎不是从customButtonStyle继承的

你知道为什么它不会继承视觉状态吗?如何将每个按钮设置为自己的缩略图

提前谢谢

下面是我的代码:

<Style x:Key="customButtonStyle" TargetType="Button">
    <Setter Property="Background" Value="{StaticResource ApplicationButtonBackgroundBrush}"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFA3AEB9" Offset="0"/>
                <GradientStop Color="#FF8399A9" Offset="0.375"/>
                <GradientStop Color="#FF718597" Offset="0.375"/>
                <GradientStop Color="#FF617584" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>

    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="Padding" Value="0"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="ControlRoot" RenderTransformOrigin="0.5,0.5">

                    <Grid.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </Grid.RenderTransform>


                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualStateGroup.Transitions>
                                <!--Take one half second to transition to the MouseOver state.-->
                                <VisualTransition To="MouseOver" GeneratedDuration="0:0:0.3"/>

                                <!-- From Button Press to Normal -->
                                <VisualTransition From="Pressed" To="Normal">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="ControlRoot">
                                            <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0.85"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:0.367" Value="1">
                                                <EasingDoubleKeyFrame.EasingFunction>
                                                    <ExponentialEase EasingMode="EaseOut" Exponent="3"/>
                                                </EasingDoubleKeyFrame.EasingFunction>
                                            </EasingDoubleKeyFrame>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="ControlRoot">
                                            <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0.85"/>
                                            <EasingDoubleKeyFrame KeyTime="0:0:0.367" Value="1">
                                                <EasingDoubleKeyFrame.EasingFunction>
                                                    <ExponentialEase EasingMode="EaseOut" Exponent="3"/>
                                                </EasingDoubleKeyFrame.EasingFunction>
                                            </EasingDoubleKeyFrame>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualTransition>
                            </VisualStateGroup.Transitions>

                            <!-- Dpad focus on button-->
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="ControlRoot">
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0.85"/>
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.367" Value="1">
                                            <EasingDoubleKeyFrame.EasingFunction>
                                                <ExponentialEase EasingMode="EaseOut" Exponent="3"/>
                                            </EasingDoubleKeyFrame.EasingFunction>
                                        </EasingDoubleKeyFrame>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="ControlRoot">
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.15" Value="0.85"/>
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.367" Value="1">
                                            <EasingDoubleKeyFrame.EasingFunction>
                                                <ExponentialEase EasingMode="EaseOut" Exponent="3"/>
                                            </EasingDoubleKeyFrame.EasingFunction>
                                        </EasingDoubleKeyFrame>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>                      
                            <!-- Button Press-->            
                            <VisualState x:Name="Pressed"/>
                            <!-- Disabled -->
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ControlRoot"/>
                                </Storyboard>
                            </VisualState>            
                            <!-- End of stateGrouop -->
                        </VisualStateGroup>


                        <!-- Focus states -->
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="FocusVisualElement">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="FocusFill">
                                        <DiscreteObjectKeyFrame KeyTime="0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility>Visible</Visibility>
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>


                            </VisualState>
                            <VisualState x:Name="Unfocused"/>

                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                    <!-- Draw border around button that is in focus -->
                    <Border x:Name="Background" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <Grid>
                            <!--<Image x:Name="ButtonImage" Width="208" Height="156" Source="{Binding tile1}"/>-->
                            <Rectangle x:Name="FocusFill" Fill="{StaticResource ApplicationButtonHoverBackgroundBrush}" Visibility="Collapsed"/>
                            <Rectangle x:Name="MouseOverFill" Fill="{StaticResource ApplicationButtonHoverBackgroundBrush}" Visibility="Collapsed"/>
                        </Grid>
                    </Border>
                    <Border x:Name="FocusVisualElement" IsHitTestVisible="false" Visibility="Collapsed" BorderBrush="{StaticResource ApplicationButtonFocusBackgroundBrush}" BorderThickness="5" Margin="-2.5"/>
                    <Border x:Name="MouseOverVisualElement" IsHitTestVisible="false" Visibility="Collapsed"  BorderBrush="{StaticResource ApplicationButtonFocusBackgroundBrush}" BorderThickness="5" Margin="-2.5"/>

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

看得见的
看得见的
///////////////////////////

<Style x:Key="tile6" TargetType="Button" BasedOn="{StaticResource customButtonStyle}">
    <Setter Property="BorderThickness" Value="3"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="0,0,1,1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">

                    <Image x:Name="ButtonImage" Width="208" Height="156" Source="{Binding tile1}"/>            

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

否。正如您自己所看到的,可视状态驻留在控件模板中,您正在提供一个全新的模板

您可以尝试将状态作为资源,并从每个模板引用它们,但我建议不要这样做。由于您有两个单独的模板,它们可能会在某个阶段分叉,在这个阶段中,重用将不是一个选项。我觉得最好还是照着做。也会使混合的生活更轻松