WPF customControl和模板继承

时间:2015-03-17 11:52:46

标签: c# wpf xaml templates custom-controls

我有点麻烦找到如何继续我的ImageButton模板。

要做的很简单:我已经按照本教程创建了一个新的customControl ImageButton:http://www.codeproject.com/Tips/773386/WPF-ImageButton

到目前为止,这么好,效果很好。

但是,在我的应用程序中,我有不同的按钮主题(在应用程序启动时已知)。我的不同按钮主题以这种方式定义:

<Style x:Key="MONDE0_ImageActionButton" TargetType="Button">

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Name="BaseBorder" CornerRadius="5" BorderThickness="1" BorderBrush="Black" Background="{DynamicResource DefaultBlueButton}">
                    <ContentPresenter VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,2,0">

                    </ContentPresenter>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="BaseBorder" Property="Background" Value="{StaticResource DefaultOverBlueButton}" />
                    </Trigger>
                    <Trigger Property="Button.IsPressed" Value="True">
                        <Setter TargetName="BaseBorder" Property="Background" Value="{StaticResource DefaultClickedBlueButton}" />
                    </Trigger>
                </ControlTemplate.Triggers>

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

问题是,当我尝试通过这样做将此主题应用于我的imgButton时:

<VegaCtr:ImageButton x:Name="btn_Send" Margin="5" Style="{DynamicResource MONDE0_actionButton}" Width="105" Height="40" ToolTip="Envoyer" Command="{Binding SendCommand}"
            Image="pack://application:,,,/Vega_WPF_Themes;component/Themes/Common/Images/Yes.png" Content="Valider"/>

我的图像按钮样式

<Style TargetType="{x:Type local:ImageButton}" x:Name="StyleImgButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:ImageButton}">
                <!-- Button Content -->
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                    <Image Source="{TemplateBinding Image}" Visibility="{TemplateBinding Image, Converter={StaticResource VisibilityConvertor}}"
                             Width="{TemplateBinding ImageWidth}" Height="{TemplateBinding ImageHeight}" Margin="0,0,5,0" />
                    <TextBlock Text="{TemplateBinding Content}" Style="{StaticResource buttonLabel}" TextWrapping="Wrap" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

imageButton的基本模板不起作用。当我删除样式属性时,它可以工作......所以,我的问题是,我怎样才能使它有效?我需要有基本的imageButton模板,并可能添加我的其他样式(带触发器等等)。 这可能吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用基于您的样式的属性来设置基本样式

<Style x:Key="MONDE0_ImageActionButton" TargetType="Button" BasedOn="{StaticResource "My_BaseStyle"}>
相关问题