LoginSignup
2
3

More than 5 years have passed since last update.

WPFのコードビハインドでStyleを記述する

Last updated at Posted at 2014-12-05

無駄にコードビハインドでStyleを記載したい場合は以下の様なコーディングできる(はず…)
※例はLineのStyleを記述

hoge.cs
    private Line GetXLine(int x, int yMax)
        {
            var lineStyle = new Style(typeof(Line));
            lineStyle.Setters.Add(new Setter(Line.StrokeThicknessProperty, 1.0));
            lineStyle.Setters.Add(new Setter(Line.StrokeProperty, new SolidColorBrush(Color.FromArgb(0, 119, 136, 153))));
            lineStyle.Setters.Add(new Setter(Line.SnapsToDevicePixelsProperty, true));

            return new Line()
            {
                X1 = x,
                Y1 = 0,
                X2 = x,
                Y2 = yMax,
                Style = lineStyle
            };
        }

グリッドの描画で使えそうなメソッド。
実際、こういう書き方を推奨しないのがWPFなのかもしれないけど
Paintクラスとかで切り出した場合に活用できるかなと。

(MVVMとか知ってればクラスに切り出しても関係ないのかな?…勉強が足りん)

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3