2

コンテンツを含まず、画像のみを含むボタンをC#で作成したいと思います。

セッターの使い方がわかりません。誰かが私を啓発できますか?

            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.Valueに何を配置する必要がありますか。「asd.jpg」

4

3 に答える 3

4

ButtonContentダンプできるプロパティがありますBitmapImage

BitmapImage image = new BitmapImage(new Uri("your source"));
TheButton.Content = image;
于 2012-06-19T13:14:07.373 に答える
1

セッターは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;
于 2012-06-19T13:16:41.617 に答える
0
   <ControlTemplate x:Key="image"> //XAML in window resources tag
            <Image Source="Save.png"/>
   </ControlTemplate>


btn.Template =  (ControlTemplate)FindResource("image");

これはより柔軟で再利用可能だと思います。

于 2012-06-19T13:46:52.900 に答える