繁体   English   中英

在运行时更改样式

[英]Change style during runtime

嗨,我有一个wpf按钮模板保存在App.xaml中。 在此按钮模板内,有一个图像控件。 我想使用此模板创建一个新按钮,并在运行时向其添加图像源。

<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>

您可以创建附加的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);
    }
}

然后在模板中使用TemplatedParent AttachedProperties.ImageSource

<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="..." .../>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM