C# WPF设置器类
我想在c#中创建一个按钮,其中没有内容,只有一个图像 我不知道如何使用setter。谁能启发我C# WPF设置器类,c#,wpf,expression,C#,Wpf,Expression,我想在c#中创建一个按钮,其中没有内容,只有一个图像 我不知道如何使用setter。谁能启发我 Setter setter = new Setter(); setter.Property = Image.SourceProperty; setter.Value = "asd.jpg"; //<--error Style Rstyle = new Style(); Rs
Setter setter = new Setter();
setter.Property = Image.SourceProperty;
setter.Value = "asd.jpg"; //<--error
Style Rstyle = new Style();
Rstyle.TargetType = typeof(Button);
Rstyle.Setters.Add(setter);
Button _Button = new Button()
{
Width = 41,
Height = 41
};
_Button.Style = Rstyle;
Setter Setter=new Setter();
setter.Property=Image.SourceProperty;
setter.Value=“asd.jpg”// 按钮
有一个内容
属性,您可以在其中转储位图图像
:
BitmapImage image = new BitmapImage(new Uri("your source"));
TheButton.Content = image;
setter不用于c#代码中,它是一个帮助器类,用于在xamls中构建样式。
只要使用这个代码
BitmapImage bmp = new BitmapImage()
bmp.BeginInit();
bmp.UriSource = new Uri("pack://application:,,,/ApplicationName;component/Resources/myImage.png");
bmp.EndInit();
Image image = new Image();
image.Source = bmp;
myButton.Content = image;
//窗口资源标记中的XAML
btn.Template=(ControlTemplate)FindResource(“图像”);
我认为这更灵活,可重复使用。@RStyle您不必这样做。BitmapImage本身就可以处理JPEG。嘿,为什么只有按照下面列出的方法(回答为“dowhilefor”)处理图像时,才会显示图像。创建位图图像后,我们需要使用图像类,然后将其分配给内容。实际上,Setter
用于触发器和样式中,无论是在C#还是在XAML中。@SLaks当然你是对的,对于这个问题,尽管它们不是必需的。
<ControlTemplate x:Key="image"> //XAML in window resources tag
<Image Source="Save.png"/>
</ControlTemplate>
btn.Template = (ControlTemplate)FindResource("image");