WPF扩展器 - 修改样式

时间:2010-11-09 09:00:35

标签: c# wpf controltemplate

我是WPF样式的新手,以及它们如何正常工作。所以这真的是一个初学者的问题。

我使用Expression Blend使用“编辑副本...”创建模板。这给了我大量的XAML语句。我继续编辑这些陈述,但没有达到我想要的效果。

我想修改Expander Up和Down States (左,右没必要)。

这是我得到的,主要来自Expression Blend:

<Window.Resources>
    <Style x:Key="ExpanderDownHeaderStyle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Padding="{TemplateBinding Padding}">
                        <Grid SnapsToDevicePixels="False" Height="25">
                            <Grid.Background>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                    <GradientStop Color="#FFEFEFEF" Offset="0.326"/>
                                    <GradientStop Color="#FF9F9F9F" Offset="1"/>
                                </LinearGradientBrush>
                            </Grid.Background>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="40"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Ellipse x:Name="circle" HorizontalAlignment="Center" Height="19" Stroke="DarkGray" VerticalAlignment="Center" Width="19"/>
                            <Path x:Name="arrow" Data="M 0,2 H 10,10"  HorizontalAlignment="Center" SnapsToDevicePixels="false" Stroke="#666" StrokeThickness="2" VerticalAlignment="Center">

                            </Path>
                            <ContentPresenter Grid.Column="1" HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Stroke" TargetName="circle" Value="#FF3C7FB1"/>
                            <Setter Property="Stroke" TargetName="arrow" Value="#222"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Stroke" TargetName="circle" Value="#FF526C7B"/>
                            <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
                            <Setter Property="Stroke" TargetName="arrow" Value="#FF003366"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ExpanderUpHeaderStyle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Padding="{TemplateBinding Padding}">
                        <Grid SnapsToDevicePixels="False" Height="25" Background="Transparent">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="40"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Ellipse x:Name="circle" HorizontalAlignment="Center" Height="19" Stroke="DarkGray" VerticalAlignment="Center" Width="19"/>
                            <Path x:Name="arrow" Data="M 0,5 H 10,10 M 5,0 L 5,10"  HorizontalAlignment="Center" SnapsToDevicePixels="false" Stroke="#666" StrokeThickness="2" VerticalAlignment="Center">
                            </Path>
                            <ContentPresenter Grid.Column="1" HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Stroke" TargetName="circle" Value="#FF3C7FB1"/>
                            <Setter Property="Stroke" TargetName="arrow" Value="#222"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Stroke" TargetName="circle" Value="#FF526C7B"/>
                            <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
                            <Setter Property="Stroke" TargetName="arrow" Value="#FF003366"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ExpanderTemplate2" TargetType="{x:Type Expander}">
       <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>

       <Setter Property="Background" Value="Transparent"/>
       <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
       <Setter Property="VerticalContentAlignment" Value="Stretch"/>
       <Setter Property="BorderBrush" Value="Black"/>
       <Setter Property="BorderThickness" Value="1"/>
       <Setter Property="Padding" Value="0,0,0,0"/>
       <Setter Property="Template">
<Setter.Value>
 <ControlTemplate TargetType="{x:Type Expander}">
  <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="3" SnapsToDevicePixels="true">
   <DockPanel>
    <ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" Content="{TemplateBinding Header}" DockPanel.Dock="Top" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" Style="{StaticResource ExpanderDownHeaderStyle}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
    <ContentPresenter x:Name="ExpandSite" DockPanel.Dock="Bottom" Focusable="false" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
   </DockPanel>
  </Border>
  <ControlTemplate.Triggers>
   <Trigger Property="IsExpanded" Value="true">
    <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
   </Trigger>
   <Trigger Property="ExpandDirection" Value="Up">
    <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Top"/>
    <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Bottom"/>
    <Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource ExpanderUpHeaderStyle}"/>
   </Trigger>
   <Trigger Property="IsEnabled" Value="false">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
   </Trigger>
  </ControlTemplate.Triggers>
 </ControlTemplate>
</Setter.Value>

                    

我修改了Path值并删除了一些东西。我还尝试为两种状态获得不同的背景(一种具有渐变,一种具有透明背景)。

任何建议我做错了什么或者应该如何做到这一点?

3 个答案:

答案 0 :(得分:0)

您需要在触发器中设置模板。

<ControlTemplate.Triggers>
   <!-- This is your Trigger Condition -->
   <Trigger Property="IsExpanded" Value="true">
      <!-- This is what happens when condition is met - Right now it is just showing the ExpandSite -->
      <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>

      <!--What is should do is modify the Template of the control to use a different one
          Not really sure what part of the control you want to modify, but this is the general idea -->
      <Setter Property="Template" Value="..." TargetName="..." />
   </Trigger>
</ControlTemplate.Triggers>

答案 1 :(得分:0)

我现在改变了方法。我想我刚刚发现Expression Blend设计器产生的代码令人恼火。

例如,我想在Up和Down时改变切换按钮的样式。我为up和down创建了一个单独的样式,并在该样式中创建了修改Button外观的触发器。

代码:

<ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButton">
    <Path Name="Triangle" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 10 10 L 20 0 Z" Fill="Black" />
    <ControlTemplate.Triggers>
        <Trigger Property="IsChecked" Value="true">
            <Setter TargetName="Triangle" Property="Data" Value="M 0 10 L 10 0 L 20 10 Z" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

我为其他想要改变的属性做了同样的事情。

答案 2 :(得分:0)

ExpanderDownHeaderStyle

自定义箭头顶部或底部修改路径

顶部箭头路径:

Data="M 0 0 L 10 10 L 20 0" 

底部箭头路径:

Data="M 20 12 L 10 0 L 0 12"