| 分類:[.NET 全般]
WPF C#ボタングラデーション変更につきまして いつもお世話になっております。
デザインの自由度からWPFにて開発することになったのですが、 開発経験があまりにも浅いため、初歩的なことで躓いております。 どうか有識者のかた、意見をお聞かせください。
○したいこと Gridに2つのボタンを配置し、各ボタンを押すたびに、押したボタンの背景色を変更させたいというものです。3パターンの背景色を順番に変更させたいです。
○質問 私なりに一応は作成することができたのですが、データバインドも使用していないうえ 、効率的な記載でないとおもわれます。下記に添付しますので、どういった構成(クラス設計)がより望ましいのかご教授いただけますでしょうか。 また別に、グラデーションのデータバインドの方法もわかるかたがいましたらご教授いただけますでしょうか。何卒よろしくお願いいたします。
namespace WpfApplication1 { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { //ボタンスタータス初期化 public int intButton1 = 1; public int intButton2 = 1;
public LinearGradientBrush button1brush; public LinearGradientBrush button2brush; public MainWindow() { InitializeComponent(); //グラデーションインスタンス生成 button1brush = new LinearGradientBrush(); button2brush = new LinearGradientBrush(); //初期のグラデーション配色設定 button1brush.GradientStops.Add(new GradientStop(Colors.Red, 0)); button1brush.GradientStops.Add(new GradientStop(Colors.Black, 1));
button2brush.GradientStops.Add(new GradientStop(Colors.Red, 0)); button2brush.GradientStops.Add(new GradientStop(Colors.Black, 1)); //位置設定 button1brush.StartPoint = new Point(0.5, 0); button1brush.EndPoint = new Point(0.5, 1); button2brush.StartPoint = new Point(0.5, 0); button2brush.EndPoint = new Point(0.5, 1); //各ボタン背景に設定 button1.Background = button1brush; button2.Background = button2brush; }
private void button1_Click(object sender, RoutedEventArgs e) { //グラデーションクリア button1brush.GradientStops.Clear(); switch (intButton1) {
case 1: //グラデーション設定 button1brush.GradientStops.Add(new GradientStop(Colors.Aqua, 0)); button1brush.GradientStops.Add(new GradientStop(Colors.White, 1)); intButton1 = 2; break; case 2: //グラデーション設定 button1brush.GradientStops.Add(new GradientStop(Colors.Black, 0)); button1brush.GradientStops.Add(new GradientStop(Colors.CadetBlue, 1)); intButton1 = 3;
break; case 3: //グラデーション設定 button1brush.GradientStops.Add(new GradientStop(Colors.Red, 0)); button1brush.GradientStops.Add(new GradientStop(Colors.Black, 1)); intButton1 = 1;
break; default:
break; } }
private void button2_Click_1(object sender, RoutedEventArgs e) { //グラデーションクリア button2brush.GradientStops.Clear(); switch (intButton2) {
case 1: //グラデーション設定 button2brush.GradientStops.Add(new GradientStop(Colors.Aqua, 0)); button2brush.GradientStops.Add(new GradientStop(Colors.White, 1)); intButton2 = 2; break; case 2: //グラデーション設定 button2brush.GradientStops.Add(new GradientStop(Colors.Black, 0)); button2brush.GradientStops.Add(new GradientStop(Colors.CadetBlue, 1)); intButton2 = 3; break; case 3: //グラデーション設定 button2brush.GradientStops.Add(new GradientStop(Colors.Red, 0)); button2brush.GradientStops.Add(new GradientStop(Colors.Black, 1));
intButton2 = 1;
break; default: break; } |