Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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# WPF设置器类_C#_Wpf_Expression - Fatal编程技术网

C# WPF设置器类

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

我想在c#中创建一个按钮,其中没有内容,只有一个图像

我不知道如何使用setter。谁能启发我

            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");