WPF为自定义控件的exists属性设置默认值

时间:2016-07-22 18:39:57

标签: c# wpf

我制作了自定义按钮,让我可以更改背景颜色normal-hover-pressed-disabled

我为所有人设置了一个新属性,默认值除了正常情况,因为它已经存在,但我不知道如何为它设置默认值

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:ProPharmacyManagerW"
                    xmlns:f="clr-namespace:ProPharmacyManagerW.Controls"
                    xmlns:vm="clr-namespace:ProPharmacyManagerW.ViewModel">
    <vm:VisibilityConvertor x:Key="VisibilityConvertor"/>
    <Style TargetType="{x:Type f:IconButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type f:IconButton}">
                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <Image x:Name="Buttonicon" 
                                   Margin="10 0"
                                   Source="{TemplateBinding Image}"
                                   Width="{TemplateBinding ImageWidth}"
                                   Height="{TemplateBinding ImageHeight}"
                                   Visibility="{TemplateBinding Image,Converter={StaticResource VisibilityConvertor}}"
                                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                   VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                   Grid.Column="0"/>
                            <TextBlock x:Name="contentText"
                                       Text="{TemplateBinding Content}"
                                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                       Margin="{TemplateBinding Padding}" 
                                       VerticalAlignment="Center"
                                       Grid.Column="1"/>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" TargetName="border" Value="{Binding ColorHover, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" TargetName="border" Value="{Binding ColorPressed, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="border" Value="{Binding ColorDisabled, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

主题

Class A{
.........
 B b;
.........
const B& getB()const{
return b;
}

};

正常背景颜色的默认值 - 不带触发器(过度按下 - 禁用) - 是无,我想将其更改为白色或能够在设计时将其从控件属性更改的内容

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你需要这样的东西:

  <Style TargetType="{x:Type f:IconButton}">
<!-- Here the default Override-->
                <Setter Property="Background" Value="DeepPink"/></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type f:IconButton}">
                            <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                    <Image x:Name="Buttonicon" 
                                       Margin="10 0"
                                       Source="{TemplateBinding Image}"
                                       Width="{TemplateBinding ImageWidth}"
                                       Height="{TemplateBinding ImageHeight}"
                                       Visibility="{TemplateBinding Image,Converter={StaticResource VisibilityConvertor}}"
                                       HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                       VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                       Grid.Column="0"/>
                                    <TextBlock x:Name="contentText"
                                           Text="{TemplateBinding Content}"
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                           Margin="{TemplateBinding Padding}" 
                                           VerticalAlignment="Center"
                                           Grid.Column="1"/>
                                </StackPanel>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter Property="Background" TargetName="border" Value="{Binding ColorHover, RelativeSource={RelativeSource TemplatedParent}}" />
                                </Trigger>
                                <Trigger Property="IsPressed" Value="true">
                                    <Setter Property="Background" TargetName="border" Value="{Binding ColorPressed, RelativeSource={RelativeSource TemplatedParent}}" />
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Background" TargetName="border" Value="{Binding ColorDisabled, RelativeSource={RelativeSource TemplatedParent}}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>