C# 在运行时更改样式

C# 在运行时更改样式,c#,wpf,xaml,C#,Wpf,Xaml,嗨,我有一个wpf按钮模板保存在App.xaml中。在这个按钮模板中,有一个图像控件。我想用这个模板创建一个新按钮,并在运行时向它添加一个图像源 <Style x:Key="newbutton" TargetType="{x:Type Button}"> <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> <Setter Pr

嗨,我有一个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);
    }
}
然后在模板中使用
AttachedProperties.ImageSource
TemplatedParent

<ControlTemplate TargetType="{x:Type Button}">
    <Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:AttachedProperties.ImageSource)}"/>
</ControlTemplate>
或者直接针对
按钮设置它

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


无法将图像源绑定到将在运行时更改的属性?按钮b=新按钮();样式s=作为样式的TryFindResource(“新按钮”);那么?如何使用AttachedProperties类使用静态
SetImageSource
方法,如
AttachedProperties.SetImageSource(b,yourImageSource)
<Button local:AttachedProperties.ImageSource="..." .../>