Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@k_kikuchi_

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

無駄にコードビハインドで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とか知ってればクラスに切り出しても関係ないのかな?…勉強が足りん)

3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
3
Help us understand the problem. What is going on with this article?