0

我是设计WPF前端的新手,请多多包涵。目前我正在为我的 XAML 应用程序创建一种样式,并且在某个时候需要帮助。我有 3 个 XAML 文件:1 个用于我的画笔绘画路径,1 个用于我的样式/颜色和 mainWindow.xaml。在我的风格中,我想创建一个自定义按钮,其中包含一个堆栈面板、一个网格和一个带有 opcityMask 填充的矩形,该填充在其上绘制了一个 visualBrush 路径。您介意帮助我了解如何访问和更改矩形填充的绘画路径吗?是否有可能附加依赖属性或其他可更改属性来更改Rectangle.OpacityMask -> VisualBrush -> 视觉路径直接来自我的 mainWindow.xaml?如何从另一个 xaml 文件的样式中访问这样的画笔绘画路径?有没有办法将新属性附加到 mainWindow.xaml 中可以设置使用的绘画路径的代码?或者您在使用样式时如何更改此类路径?非常感谢你们非常感谢您的帮助!如果某些事情没有 100% 准确地描述,请告诉我。

这是我的代码:

主窗口.xaml

<Window x:Class="SynchDepots.MainWindow"
    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"
    xmlns:local="clr-namespace:SynchDepots"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources\Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
[...]
<StackPanel>
    <Grid>
[...]
        **<Button Grid.Column="0" Grid.Row="1" x:Name="btnTestForNewButtons" Style="{StaticResource DefaultButton2}"  Content="Test button" />**

    </Grid>
    <Grid>
[...]
    </Grid>
</StackPanel>

样式.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:SynchDepots">


<!-- ###############
###### COLORS ######
################ -->
<!--#region COLORS-->

<!--Colors-->
<!--
    Work related:
    ColorCCBlueDark      #FF24387F
    ColorCCBlueLight     #FF009DDC
    ##############################
    Private related: 

-->
<Color x:Key="ColorAccentDark">#FF24387F</Color>
<Color x:Key="ColorAccentLight">#FF009DDC</Color>

<SolidColorBrush x:Key="ColorColoredDefaultText" Color="White" />
<SolidColorBrush x:Key="ColorAccentDarkSolid" Color="{StaticResource ColorAccentDark}"/>
<SolidColorBrush x:Key="ColorAccentLightSolid" Color="{StaticResource ColorAccentLight}"/>
<SolidColorBrush x:Key="ColorBackgroundDarkSquare" Color="#E01B1B1B" />
<SolidColorBrush x:Key="ColorBackgroundLightSquare" Color="#E0212121" />

<!--#endregion-->


<!-- ###############
###### BUTTONS #####
################ -->
<!--#region BUTTONS-->
<!--DefaultButton2-->
<Style x:Key="DefaultButton2" TargetType="Button">
    <Setter Property="Margin" Value="2 2 2 2"/>
    <Setter Property="Height" x:Name="BtnHeight" Value="20"/>
    <Setter Property="Width" x:Name="BtnWidth" Value="120"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="FontWeight" Value="Light"/>
    <Setter Property="Foreground" Value="White"/>

    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" Opacity="0.6">
                <GradientStop Color="#D83A3A3A" Offset="0"/>
                <GradientStop Color="#D8686868" Offset="1"/>
                <GradientStop Color="#D8363636" Offset="0.4"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect BlurRadius="4"
                          Direction="315"
                          Color="#FF878787"
                          RenderingBias="Quality" 
                          Opacity="0.4" 
                          ShadowDepth="5"/>
        </Setter.Value>
    </Setter>

    <Setter Property="Template">
        <Setter.Value>

            <ControlTemplate TargetType="Button">
                <Grid x:Name="pr7">

                    <StackPanel x:Name="btnStackPanel">
                        <Border Background="{TemplateBinding Background}"
                        BorderThickness="1"
                        CornerRadius="1">
                            <Border.BorderBrush>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF464646" Offset="0.6"/>
                                    <GradientStop Color="{StaticResource ColorAccentLight}" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.BorderBrush>
                            <Grid x:Name="pr8">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="20"/>
                                    <ColumnDefinition Width="1"/>
                                    <ColumnDefinition Width="99"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="18"/>
                                </Grid.RowDefinitions>

                                <Rectangle Grid.Row="0" Grid.ColumnSpan="2" x:Name="rctImg">
                                    <Rectangle.Width>16</Rectangle.Width>
                                    <Rectangle.Height>16</Rectangle.Height>
                                    <Rectangle.Fill>
                                            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                                <GradientStop Color="{StaticResource ColorAccentLight}" Offset="1" />
                                                <GradientStop Color="{StaticResource ColorAccentDark}" Offset="0" />
                                            </LinearGradientBrush>
                                    </Rectangle.Fill>
                                    <Rectangle.OpacityMask>
                                        <VisualBrush x:Name="opacityMaskVisualPath" Stretch="Uniform" Visual="**{StaticResource imgBtnPreferencesTune}**"/>
                                    </Rectangle.OpacityMask> 
                                </Rectangle>
                                <Rectangle x:Name="btnRectangleSplitter" Grid.Column="1" Width="1" Height="Auto" HorizontalAlignment="Center">
                                    <Rectangle.Fill>
                                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                            <GradientStop Color="#00959595" Offset="0.05"/>
                                            <GradientStop Color="#FF727272" Offset="0.4"/>
                                            <GradientStop Color="#FF727272" Offset="0.6"/>
                                            <GradientStop Color="#00959595" Offset="0.95"/>
                                        </LinearGradientBrush>
                                    </Rectangle.Fill>
                                </Rectangle>
                                <TextBlock x:Name="btnTxtContent" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" Text="{TemplateBinding Content}"></TextBlock>

                            </Grid>
                        </Border>
                    </StackPanel>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

[...]

<!--#endregion-->

4

1 回答 1

0

我解决了这个问题如下: - 分离所有元素并将它们放入自己的样式中;- 将每个 VisualBrush -> Visual 放入单个样式中,链接到 Path/Data 样式;- 将按钮设计成单一样式;- 在 MainWindow.xaml 中一一引用样式:

主窗口.xaml

<Window x:Class="SynchDepots.MainWindow"
    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"
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:local="clr-namespace:SynchDepots"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources\Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

[...]
        <Button Grid.Column="1" Grid.Row="1" Style="{StaticResource DefaultButton}">
            <StackPanel Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" Orientation="Horizontal" Margin="0" Width="118" Height="18">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="1"/>
                        <ColumnDefinition Width="97"/>
                    </Grid.ColumnDefinitions>
                    <Rectangle Grid.Column="0" Style="{StaticResource rctForImgBtnPreferencesGearWheel}"/>
                    <Rectangle Grid.Column="1" Style="{StaticResource rctSplitter}"/>
                    <TextBlock Grid.Column="2" VerticalAlignment="Center" TextAlignment="Center">Geiler Scheiß</TextBlock>
                </Grid>
            </StackPanel>
        </Button>

引用单一样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:SynchDepots">


<!-- ###############
###### COLORS ######
################ -->
<!--#region COLORS-->

<!--Colors-->
<!--
    Work related:
    ColorCCBlueDark      #FF24387F
    ColorCCBlueLight     #FF009DDC
    ##############################
    Private related: 

-->
<Color x:Key="ColorAccentDark">#FF24387F</Color>
<Color x:Key="ColorAccentLight">#FF009DDC</Color>

<SolidColorBrush x:Key="ColorColoredDefaultText" Color="White" />
<SolidColorBrush x:Key="ColorAccentDarkSolid" Color="{StaticResource ColorAccentDark}"/>
<SolidColorBrush x:Key="ColorAccentLightSolid" Color="{StaticResource ColorAccentLight}"/>
<SolidColorBrush x:Key="ColorBackgroundDarkSquare" Color="#E01B1B1B" />
<SolidColorBrush x:Key="ColorBackgroundLightSquare" Color="#E0212121" />

<!--#endregion-->

<!-- ###############
####### TEXTS ######
################ -->
<!--#region TEXTS-->


<!--ColoredDetaultText-->
<Style x:Key="WhiteText" TargetType="TextBlock">
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="Foreground" Value="{StaticResource ColorColoredDefaultText}"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="FontStyle" Value="Normal"/>
</Style>

<!--#endregion-->

<!-- ###############
###### BUTTONS #####
################ -->
<!--#region BUTTONS-->

<!--DefaultButton-->
<Style x:Key="DefaultButton" TargetType="Button">
    <Setter Property="Margin" Value="2 2 2 2"/>
    <Setter Property="Height" x:Name="BtnHeight" Value="20"/>
    <Setter Property="Width" x:Name="BtnWidth" Value="120"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="FontWeight" Value="Light"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}"
                        BorderThickness="1"
                        CornerRadius="1">
                    <Border.BorderBrush>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF464646" Offset="0.6"/>
                            <GradientStop Color="{StaticResource ColorAccentLight}" Offset="1"/>
                        </LinearGradientBrush>
                    </Border.BorderBrush>
                    <ContentPresenter HorizontalAlignment="Center" 
                                      VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" Opacity="0.6">
                <GradientStop Color="#FF3A3A3A" Offset="0"/>
                <GradientStop Color="#FF686868" Offset="1"/>
                <GradientStop Color="#FF363636" Offset="0.4"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect BlurRadius="4"
                          Direction="315"
                          Color="#FF878787"
                          RenderingBias="Quality" 
                          Opacity="0.4" 
                          ShadowDepth="5"/>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" Opacity="0.65">
                        <GradientStop Color="#FF292929" Offset="0"/>
                        <GradientStop Color="{StaticResource ColorAccentLight}" Offset="1"/>
                        <!--<GradientStop Color="#FFA8A8A8" Offset="1"/>-->
                        <GradientStop Color="#FF363636" Offset="0.4"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" Opacity="0.7">
                        <GradientStop Color="#FF282828" Offset="0"/>
                        <GradientStop Color="#FF3C3C3C" Offset="1"/>
                        <GradientStop Color="#FF282828" Offset="0.4"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Trigger>
        <EventTrigger RoutedEvent="MouseEnter">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Duration="0:0:0.200" 
                                         Storyboard.TargetProperty="FontSize" To="13"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
        <EventTrigger RoutedEvent="MouseLeave">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Duration="0:0:0.800" 
                                         Storyboard.TargetProperty="FontSize" To="12" 
                                         AccelerationRatio="0.4"
                                         DecelerationRatio="0.6"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Style.Triggers>
</Style>

<!--DefaultButton2-->
<Style x:Key="DefaultButton2" TargetType="Button">
    <Setter Property="Margin" Value="2 2 2 2"/>
    <Setter Property="Height" x:Name="BtnHeight" Value="20"/>
    <Setter Property="Width" x:Name="BtnWidth" Value="120"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="FontWeight" Value="Light"/>
    <Setter Property="Foreground" Value="White"/>

    <Setter Property="Background">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" Opacity="0.6">
                <GradientStop Color="#D83A3A3A" Offset="0"/>
                <GradientStop Color="#D8686868" Offset="1"/>
                <GradientStop Color="#D8363636" Offset="0.4"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect BlurRadius="4"
                          Direction="315"
                          Color="#FF878787"
                          RenderingBias="Quality" 
                          Opacity="0.4" 
                          ShadowDepth="5"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="btnGridRoot">
                    <StackPanel x:Name="btnStackPanel">
                        <Border Background="{TemplateBinding Background}"
                        BorderThickness="1"
                        CornerRadius="1">
                            <Border.BorderBrush>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FF464646" Offset="0.6"/>
                                    <GradientStop Color="{StaticResource ColorAccentLight}" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.BorderBrush>

                            <Grid x:Name="btnGridForContent" Width="120" Height="18">

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="20"/>
                                <ColumnDefinition Width="1"/>
                                <ColumnDefinition Width="99"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="18"/>
                            </Grid.RowDefinitions>

                            </Grid>

                        </Border>
                    </StackPanel>

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

    <Style.Triggers>
        [...]
    </Style.Triggers>
</Style>

<!--#endregion-->

<!-- ###############
##### CONTENTS #####
################ -->
<!--#region CONTENTS -->




<!--#endregion-->

<!-- ###############
## SHAPES / FORMS ##
################ -->
<!--#region SHAPES / FORMS-->



<!--Main Rectangle for Button Images-->
<Style x:Key="rctBaseForImg" TargetType="Rectangle">
    <Setter Property="Width" Value="16"/>
    <Setter Property="Height" Value="16"/>
    <Setter Property="Margin" Value="2,1"/>
    <Setter Property="Fill">
        <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                    <GradientStop Color="{StaticResource ColorAccentLight}" Offset="1" />
                    <GradientStop Color="{StaticResource ColorAccentDark}" Offset="0" />
                </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>

<!--Rectangle for seperator-->
<Style x:Key="rctSplitter" TargetType="Rectangle">
    <Setter Property="Width" Value="1"/>
    <Setter Property="Height" Value="18"/>
    <Setter Property="Fill">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#00959595" Offset="0.05"/>
                <GradientStop Color="#FF727272" Offset="0.4"/>
                <GradientStop Color="#FF727272" Offset="0.6"/>
                <GradientStop Color="#00959595" Offset="0.95"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>


<!--Rectangle for path imgBtnPreferencesGearWheel-->
<Style x:Key="rctForImgBtnPreferencesGearWheel" BasedOn="{StaticResource rctBaseForImg}" TargetType="Rectangle">
    <Setter Property="OpacityMask">
        <Setter.Value>
            <VisualBrush Stretch="Uniform" Visual="{StaticResource imgBtnDefault}"/>
        </Setter.Value>
    </Setter>
</Style>


<!--#endregion-->

这引用了我的 Icons.xaml 中记录的路径:

<!--imgBtnPreferences // Settings-->
<Path x:Key="imgBtnDefault"
      Data="M10 17l5-5-5-5v10z"
      Fill="White" />
[...]

于 2018-12-06T13:50:01.353 回答