3

具体的な質問は、カスタム スタイル「水」から継承し、その「Grid.Background」部分を上書きする方法ですか?

可能性はまったくありますか?まず、そこに別の画像を置きたいだけです。

ButtonStyles1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    <Style x:Key="Water" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="Grid" ShowGridLines="False" d:DesignWidth="150" d:DesignHeight="150">
                        <Grid.Background>
                            <ImageBrush ImageSource="/WpfControlLibrary_Battleship;component/Resources/ButtonBackgrounds/Water.jpg"/>
                        </Grid.Background>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="MouseOver" GeneratedDuration="0">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathNW"/>
                                            <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathNE"/>
                                            <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathSW"/>
                                            <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathSE"/>
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathNW">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathNE">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathSW">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PathSE">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Path x:Name="PathNW" Data="M2.5,2.5 L82.833332,2.5 M2.5000006,2.5 L2.5000006,82.833329" HorizontalAlignment="Stretch" Height="Auto" Margin="0" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
                        <Path x:Name="PathNE" Grid.Column="6" Data="M2.5,2.5 L82.833356,2.5 M82.833333,2.5 L82.833333,82.833335" HorizontalAlignment="Stretch" Height="Auto" Margin="0" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
                        <Path x:Name="PathSW" Data="M2.5,2.5 L2.5,82.833328 M2.5,82.833323 L82.833336,82.833323" HorizontalAlignment="Stretch" Margin="0" Grid.Row="6" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
                        <Path x:Name="PathSE" Grid.Column="6" Data="M2.5000001,168.16667 L168.16665,168.16667 M168.16666,2.5000051 L168.16666,168.1667" HorizontalAlignment="Stretch" Height="Auto" Margin="0" Grid.Row="6" StrokeStartLineCap="Square" Stretch="Fill" StrokeEndLineCap="Square" Stroke="#FF9B0000" StrokeThickness="5" VerticalAlignment="Stretch" Width="Auto" Grid.ColumnSpan="2" Grid.RowSpan="2" Opacity="0"/>
                        <Rectangle x:Name="Rectangle" Grid.ColumnSpan="8" Fill="Red" Margin="0" Grid.RowSpan="8" Stroke="Black" Opacity="0.495">
                            <Rectangle.Visibility>
                                <Visibility>Hidden</Visibility>
                            </Rectangle.Visibility>
                        </Rectangle>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="Bow_W" TargetType="{x:Type Button}" BasedOn="{StaticResource Water}">
        <!--How to inherit from the style "Water" and to overwrite the "Grid.Background"-Part?-->
    </Style>
</ResourceDictionary>
4

2 に答える 2

4

テンプレートを外部プロパティにバインドする必要があり、スタイルでそのプロパティをオーバーライドできます。例:

<Style x:Key="Water" TargetType="Button">
    <Setter Property="Background">
        <Setter.Value>
            <ImageBrush ImageSource="/WpfControlLibrary_Battleship;component/Resources/ButtonBackgrounds/Water.jpg"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Grid">
                <Grid Background="{TemplateBinding Background}" ...
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="SomeOtherWaterStyle" BasedOn="{StaticResource WaterStyle" TargetType="Grid">
    <Setter Property="Background" Value="Red"/>
</Style>

テンプレート バインドするプロパティの実際の選択は、ユーザー次第であることに注意してください。この例で使用することはおそらく理にかなってBackgroundいます。

于 2012-05-20T08:06:29.393 に答える
1

次のようなbasedonプロパティをいつでも使用できます。

<Style x:Key="NewSytle" BasedOn="{StaticResource Water}">
  <Setter Property="Control.Background" Value="Blue"/>
</Style>
于 2012-05-20T11:33:09.563 に答える