Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态更改样式的属性_C#_Wpf_Xaml - Fatal编程技术网

C# 动态更改样式的属性

C# 动态更改样式的属性,c#,wpf,xaml,C#,Wpf,Xaml,我正在开发WPF应用程序。我有一个资源字典,其中我为工具提示和按钮编写了自定义样式。实际上,我为按钮做了两种样式。 其中一个,包含了一个显示在按钮内容左侧的图像 <Style x:Key="ButtonImageStyle" TargetType="{x:Type Button}"> ........ <TextBlock Margin="5.25,2.417,5.583,5.25" Foreground = White /> <Image x

我正在开发WPF应用程序。我有一个资源字典,其中我为工具提示和按钮编写了自定义样式。实际上,我为按钮做了两种样式。 其中一个,包含了一个显示在按钮内容左侧的图像

<Style x:Key="ButtonImageStyle" TargetType="{x:Type Button}">
     ........
   <TextBlock Margin="5.25,2.417,5.583,5.25" Foreground = White />
   <Image x:Name="ButtonImage" Source="/MyProject;component/Images/icoMainMenu01.png" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="-100,0,0,0" Width="16" Height="16"/>
   .... </Style

........

.... 有多种选择,从(ab)使用属性(如)到在a中子类化或组合,您还可以创建一个

最干净的可能是子类化,然后可以为要使用的创建一个新的子类,然后可以使用

要使用VS进行子类化,请从新项中选择
自定义控件(WPF)
,这将创建一个类文件,并向主题资源字典添加一个基本样式,该字典通常位于
主题/Generic.xaml
中。该类将如下所示:

//<Usings>

namespace Test.Controls
{
    public class ImageButton : Button
    {
        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));
        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }
    }
}
//
命名空间测试控件
{
公共类ImageButton:按钮
{
公共静态只读DependencyProperty ImageProperty=
DependencyProperty.Register(“图像”、类型化(ImageSource)、类型化(ImageButton)、新UIPropertyMetadata(null));
公共图像源图像
{
获取{return(ImageSource)GetValue(ImageProperty);}
set{SetValue(ImageProperty,value);}
}
}
}
现在,主题将更加复杂,您只需为按钮复制一个主题,然后将其粘贴到默认样式中。然后,您只需将图像添加到某个位置,但要使用此绑定:

<Image Source="{TemplateBinding Image}" .../>

使用此控件后,您将不再需要引用样式,因为所有内容都是默认样式,因此图像现在有一个属性:

<controls:ImageButton Content="Lorem Ipsum"
                      Image="Img.png"/>

(要使用
标记
,只需使用普通按钮,并使用模板绑定到标记,然后将按钮的标记设置为URL)



我忘了提到另一种使用动态资源的可能性,它有点冗长,但非常简单,请参见我的示例。

Wow。谢谢你的回答。问题是我对WPF有点陌生。你能为我的问题提供一个简单的例子吗?这会让我的生活更轻松谢谢。在你回答之前,我一直在谈论别的事情。非常感谢!:)@MihaiuAdrian:添加了一个示例,我没有再次使其独立,对此非常抱歉,但我不想复制整个默认按钮模板。非常感谢!我会尽一切努力,直到成功为止。非常感谢。祝你一切顺利DSo我看了一个冗长但非常简单的示例,花了2分钟的时间使我的代码适应您的回答。非常感谢你。真是太棒了D
<controls:ImageButton Content="Lorem Ipsum"
                      Image="Img.png"/>