在运行时更改样式

时间:2015-08-02 17:11:38

标签: c# wpf xaml

您好我在App.xaml中保存了一个wpf按钮模板。在此按钮模板中,有一个图像控件。我想用这个模板创建一个新按钮,并在运行时为它添加一个图像源。

<Style x:Key="newbutton" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                        !!!!!!!!!!!!!THIS GUY HERE>>>>>>>>>>>>>>>><Image HorizontalAlignment="Left" Height="17.96" VerticalAlignment="Top" Width="71"/>**
                    </Themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                            <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:1)

您可以创建附加的DependencyProperty

public static class AttachedProperties
{
    public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.RegisterAttached("ImageSource", typeof(ImageSource), typeof(AttachedProperties), new UIPropertyMetadata(null));

    public static void SetImageSource(DependencyObject d, ImageSource source)
    {
        d.SetValue(ImageSourceProperty, source);
    }

    public static ImageSource GetImageSource(DependencyObject d)
    {
        return (ImageSource)d.GetValue(ImageSourceProperty);
    }
}

然后在模板中使用AttachedProperties.ImageSource

TemplatedParent
<ControlTemplate TargetType="{x:Type Button}">
    <Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:AttachedProperties.ImageSource)}"/>
</ControlTemplate>

然后您可以在Setter

中进行设置
<Setter Property="local:AttachedProperties.ImageSource" Value="..."/>

或直接针对Button

设置
<Button local:AttachedProperties.ImageSource="..." .../>